Run yii controller/action on command line

╄→尐↘猪︶ㄣ 提交于 2019-12-23 19:08:55

问题


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

CI style: php index.php controller action


回答1:


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.




回答2:


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



回答3:


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

    // ...

}


来源:https://stackoverflow.com/questions/16300788/run-yii-controller-action-on-command-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!