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

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

报告相同问题?

悬赏问题

  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 filenotfounderror:文件是存在的,权限也给了,但还一直报错
  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据