使用Yii CRON命令在控制器中执行和操作

时间:2018-11-01 10:31:13

标签: yii cron

我在控制器MyController内有一个动作:

public function actionMyAction()
{
//To do
}

我也有一个控制台命令类/protected/commands/TestMyCommand.php

class TestMyCommand extends CConsoleCommand {

public function actionDoMyCommand($args) {
    $class = new MyController;
    $result = $class->actionMyAction();
}

}

在我的yiiBase.php中,有以下一行:

                else
                include($className.'.php');

哪个抛出错误:

PHP Error[2]: include(MyController.php): failed to open stream: No such file or directory

运行此命令时。

php /var/www/html/path/protected/yiic.php testmycommand domycommand

当我尝试在共享文件夹中调用任何其他php文件时,不会引发错误。 有没有一种方法可以执行我的动作而无需将功能转移到共享文件夹中的文件?

1 个答案:

答案 0 :(得分:0)

我最终在usd_value文件夹中创建了一个MyActionUtils.php,并将逻辑从控制器转移到了文件中。

libraries/shared

然后我将命令更改为:

class MyActionUtils
{

public function __construct()
{

}

public function actionMyAction()
{ 
  //To do
}

然后我通过导航到包含class TestMyCommand extends CConsoleCommand { public function actionDoMyCommand($args) { $class = new MyActionUtils; $result = $class->actionMyAction(); } } 的文件夹并运行以下命令来调用cron:yiic.php

相关问题