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条)

报告相同问题?

悬赏问题

  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开