This method parses the specified route and creates the corresponding child module(s), controller and action
instances. It then calls [[Controller::runAction()]] to run the action with the given parameters.
If the route is empty, the method will use [[defaultRoute]].
For example, to run public function actionTest($a, $b) assuming that the controller has options the following
code should be used:
php
\Yii::$app->runAction('controller/test', ['option' => 'value', $a, $b]);
public runAction ( string $route, array $params = [] ) : integer | Response | ||
$route | string | the route that specifies the action. |
$params | array | the parameters to be passed to the action |
return | integer | Response | the result of the action. This can be either an exit code or Response object. Exit code 0 means normal, and other values mean abnormal. Exit code of `null` is treaded as `0` as well. |
public function runPharCommand() { $configuration = (require \Yii::getAlias('@tests/unit/_config.php')); $configuration['modules']['phar'] = $this->moduleConfiguration; $application = new Application($configuration); $application->requestedRoute = 'phar/build'; $application->runAction('phar/build', []); }
/** * In addition to the default behavior of runAction, the console command * will strip the first element of the route and threat it as a module * changed the controller namespace to run the commands. * * ``` * ./vendor/bin/luya <module>/<commandController>/<commandAction> * ``` * * Will run all controllers located in the `commands` folder of a module. * * {@inheritDoc} * * @see \yii\console\Application::runAction() * @since 1.0.0-beta6 */ public function runAction($route, $params = []) { // In addition to the default behavior of runAction, the console command // will strip the first element of the route and threat it as a module // changed the controller namespace to run the commands if (!empty($route)) { $partial = explode("/", $route); // if there is a first key in the splitted array if (isset($partial[0]) && count($partial) > 1 && ($module = Yii::$app->getModule($partial[0]))) { try { // change the controller namespace of this module to make usage of `commands`. $module->controllerNamespace = $module->namespace . '\\commands'; unset($partial[0]); // action response return $module->runAction(implode("/", $partial), $params); } catch (\Exception $e) { throw new Exception("Exception in route \"{$route}\": \"{$e->getMessage()}\" in file \"{$e->getFile()}\" on line {$e->getLine()}.", 0, $e); } } } // call parent action return parent::runAction($route, $params); }