yii\base\Module::runAction PHP Method

runAction() public method

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]].
public runAction ( string $route, array $params = [] ) : mixed
$route string the route that specifies the action.
$params array the parameters to be passed to the action
return mixed the result of the action.
    public function runAction($route, $params = [])
    {
        $parts = $this->createController($route);
        if (is_array($parts)) {
            /* @var $controller Controller */
            list($controller, $actionID) = $parts;
            $oldController = Yii::$app->controller;
            Yii::$app->controller = $controller;
            $result = $controller->runAction($actionID, $params);
            if ($oldController !== null) {
                Yii::$app->controller = $oldController;
            }
            return $result;
        } else {
            $id = $this->getUniqueId();
            throw new InvalidRouteException('Unable to resolve the request "' . ($id === '' ? $route : $id . '/' . $route) . '".');
        }
    }

Usage Example

Example #1
2
 /**
  * Run the job.
  * 
  * @param Job $job
  */
 public function run(Job $job)
 {
     \Yii::info('Running job', 'yii2queue');
     try {
         if ($job->isCallable()) {
             $retval = $job->runCallable();
         } else {
             $retval = $this->module->runAction($job->route, $job->data);
         }
     } catch (\Exception $e) {
         if ($job->isCallable()) {
             if (isset($job->header['signature']) && isset($job->header['signature']['route'])) {
                 $id = $job->id . " " . \yii\helpers\Json::encode($job->header['signature']['route']);
             } else {
                 $id = $job->id . ' callable';
             }
         } else {
             $id = $job->route;
         }
         \Yii::error("Fatal Error: Error running route '{$id}'. Message: {$e->getMessage()}", 'yii2queue');
         throw new \yii\base\Exception("Error running route '{$id}'. Message: {$e->getMessage()}. File: {$e->getFile()}[{$e->getLine()}]. Stack Trace: {$e->getTraceAsString()}", 500);
     }
     if ($retval !== false) {
         \Yii::info('Deleting job', 'yii2queue');
         $this->delete($job);
     }
 }
All Usage Examples Of yii\base\Module::runAction