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'
],
],
];
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();
}
}
It's very rare task for Yii2, but you may use this as an example:
// Here we call @frontend/site/index from @console/controller.
\Yii::$app->controllerNamespace = 'frontend\controllers'; //change current controller
\Yii::$app->runAction('site/index'); //run the Action
But, you may have an error about CSRF, cause App's controller (yii\webcontroller) is not the same as Console's controller (yii\console\controller)!
ps. if you don't change controller's namespace, runAction won't find desired Action.