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

Use Yii CRON command to execute and action in a Controller

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

public function actionMyAction()
{
//To do
}

我还有一个控制台命令class /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 文件时,不会抛出错误。 有没有一种方法可以在不将功能转移到共享文件夹中的文件的情况下执行我的操作?

我在 libraries/shared 文件夹中创建了一个 MyActionUtils.php 并将逻辑从控制器传输到文件。

class MyActionUtils
{

public function __construct()
{

}

public function actionMyAction()
{ 
  //To do
}

然后我将命令更改为:

class TestMyCommand extends CConsoleCommand {

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

}

然后我通过导航到包含 yiic.php 和 运行 命令的文件夹来调用我的 cron:./yiic testmycommand