如何在 Yii 的 Web 应用程序操作中调用控制台命令? [英] How to call a console command in web application action in Yii?

查看:22
本文介绍了如何在 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个参数必须设置但没有影响,它们用于使用控制台时的--help选项.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆