CodeIgniter : Run a script from command line if the controller is inside a folder?
Asked Answered
T

3

5

As stated here : http://codeigniter.com/user_guide/general/cli.html

A page like : http://www.example.com/myController/myFunc/myParam

can be run on the command line as :

php index.php myController myFunc myParam

My codeignitor set up has some folders to group the controllers, lets say like this :

myFolder -> myPageController
         -> myAdminController

So, the url becomes :

http://www.example.com/myFolder/myController/myFunc/myParam

How do I call the same thing on CLI ? Something like :

php index.php "myFolder/myController" myFunc myParam

Does not seem to work.

Tatter answered 14/2, 2012 at 11:46 Comment(1)
Any idea how to execute example.com/beta/app/index.php/notification/test/121 via CLIAmative
M
16

You can try with :

php index.php myFolder myController myFunc myParam1 myParam2 ...
Moro answered 14/2, 2012 at 11:58 Comment(2)
This is not working in my case... :( Maybe another suggestion on how I can fix this?Orth
This is not working in case also .. Please suggest solution for this.Hobbema
P
1

Note, the index method in class file is necessary for work properly in CLI, otherwise codeigniter will return 404 error.

;)

Pervert answered 20/11, 2012 at 11:29 Comment(1)
oh my .. ! Plz help, How can i fix it? return 404 error =.=Humbert
R
1

For CodeIgniter 1.7 (if someone is unlucky to have to support a legacy project), there is a solution mentioned here:

Running CodeIgniter from the Command Line

The Goal

Just like the title says, our goal is to be able to run CodeIgniter applications from the command line. This is necessary for building cron jobs, or running more intensive operations so you don't have the resource limitations of a web script, such as maximum execution time.

This is what it looks like on my local Windows machine: Screenshor from the original post

The above code would be like calling this URL:

http://www.example.com/hello/world/foo

The Hack

Create a "cli.php" file at the root of your CodeIgniter folder:

if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Command Line Only!');
}



set_time_limit(0);

$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'] = $argv[1];

require dirname(__FILE__) . '/index.php';

If you are on a Linux environment and want to make this script self executable, you can add this as the first line in cli.php:

#!/usr/bin/php

If you want a specific controller to be command line only, you can block web calls at the controller constructor:

class Hello extends Controller {

    function __construct() {
        if (isset($_SERVER['REMOTE_ADDR'])) {
            die('Command Line Only!');
        }
        parent::Controller();
    }

    // ...

}
Rictus answered 19/11, 2018 at 13:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.