CodeIgniter : Run a script from command line if the controller is inside a folder? CodeIgniter : Run a script from command line if the controller is inside a folder? codeigniter codeigniter

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


You can try with :

php index.php myFolder myController myFunc myParam1 myParam2 ...


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

;)


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();    }    // ...}