douzhang3898 2017-11-27 15:15
浏览 547
已采纳

控制台Yii2中的runAction模块

i have in console\controllers

    class TestController extends Controller
{


    public function actionTest()
    {
        Yii::$app->runAction('how?');

    }

}

and

backend\modules\mytestmodule\controllers\MyTestControllers

    public function actionCreate()
    {

        echo 1;

    }

Is it possible to call runAction "actionCreate" in console "actionTest" ? Yii::$app->runAction('/backend/modules/mytestmodule/MyTest/actionCreate'); dont work

console config

return [
'id' => 'console',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'console\controllers',
'controllerMap' => [
    'command-bus' => [
        'class' => 'trntv\bus\console\BackgroundBusController',
    ],
    'message' => [
        'class' => 'console\controllers\ExtendedMessageController'
    ],
    'migrate' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationPath' => '@common/migrations/db',
        'migrationTable' => '{{%system_db_migration}}'
    ],
    'rbac-migrate' => [
        'class' => 'console\controllers\RbacMigrateController',
        'migrationPath' => '@common/migrations/rbac/',
        'migrationTable' => '{{%system_rbac_migration}}',
        'templateFile' => '@common/rbac/views/migration.php'
    ],
],

];

  • 写回答

2条回答 默认 最新

  • duanba3707 2017-11-27 16:52
    关注

    Ok i found a solution but remember that the structure you desired isn't a good solution. Becouse you try to use \Yii::$app->runAction() in a console context so your \Yii::$app is an istance of ConsoleApplication and not WebApplication and the actions you can use in this context is different from the Web context. But you can call a web context action in a console context with this workaround but i repeat that isn't a good idea for futher issue will be present:

    use backend\modules\mytestmodule\controllers\MyTestController;
    
    class TestController extends Controller
    {
    
    
        public function actionTest()
        {
            //Yii::$app->runAction('how?'); you cannot use here run action for access an action in web context
            $controller = new MyTestController('mytest', $this->module);
            $controller->actionCreate();
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#单片机#的问题:以ATMEGA128或相近型号单片机为控制器设计直流电机调速的闭环控制系统(相关搜索:设计报告|软件设计|流程图)
  • ¥15 打开软件提示错误:failed to get wglChoosePixelFormatARB
  • ¥30 电脑误删了手机的照片怎么恢复?
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办