如何在Yii中的Web应用程序操作中调用控制台命令? [英] How to call a console command in web application action in Yii?
本文介绍了如何在Yii中的Web应用程序操作中调用控制台命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个控制台命令来执行消费者时间,而且我需要知道如何在YII的Web应用程序操作中调用(执行)它。
I have a console command to do a consumer time, AND I need to know how to call (execute) it in a web application action in YII.
class MyCommand extends CConsoleCommand{
public function actionIndex(){
$model = new Product();
$model->title = 'my product';
...
$model->save();
.
.
.
}
}
我想执行此代码。
推荐答案
尝试以下操作:
Yii::import('application.commands.*');
$command = new MyCommand("test", "test");
$command->run(null);
必须设置值 test的2个参数,但不产生影响,使用它们
The 2 parameters with value "test" must be set but do not have an impact, they are used for the --help option when using the console.
/**
* Constructor.
* @param string $name name of the command
* @param CConsoleCommandRunner $runner the command runner
*/
public function __construct($name,$runner)
{
$this->_name=$name;
$this->_runner=$runner;
$this->attachBehaviors($this->behaviors());
}
https://github.com/yiisoft/yii/blob/master/framework/console/CConsoleCommand.php#L65
这篇关于如何在Yii中的Web应用程序操作中调用控制台命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文