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

控制台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条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题