Onur Degerli
Onur Degerli

Reputation: 219

Run yii controller/action on command line

Is it possible to run yii controller/action on linux command line just like CodeIgniter usage?

CI style: php index.php controller action

Upvotes: 6

Views: 7801

Answers (3)

Miomir Dancevic
Miomir Dancevic

Reputation: 6852

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

    // ...

}

Upvotes: 0

skyoo
skyoo

Reputation: 31

class NotifyUnsharedItemsCommand extends CConsoleCommand
{
    public function run($args)
    {
        $action = Yii::createComponent('application.controllers.actions.NotifyUnsharedItemsAction',$this,'notify');
        $action->run();
    }
 }

Upvotes: 3

Kevin
Kevin

Reputation: 513

I'm not aware of running controller/action from the command line apart from making a GET request, however there are yii console applications (as opposed to web applications) that you might consider taking a look at here http://www.yiiframework.com/doc/guide/1.1/en/topics.console. I'm not sure what you are trying to achieve so it's hard to know whether or not it will work for you.

Yii console applications are derived from the same base classes as your web application so you can use the same resources as your webapp.

Upvotes: 1

Related Questions