/** * Runs an action within this controller with the specified action ID and parameters. * If the action ID is empty, the method will use [[defaultAction]]. * @param string $id the ID of the action to be executed. * @param array $params the parameters (name-value pairs) to be passed to the action. * @return mixed the result of the action. * @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully. * @see createAction() */ public function runAction($id, $params = []) { $action = $this->createAction($id); if ($action === null) { throw new InvalidRouteException('Unable to resolve the request: ' . $id); } Application::trace('Route to run: ' . $id, __METHOD__); if (Application::$app->requestedAction === null) { Application::$app->requestedAction = $action; } $this->action = $action; $result = null; // run the action $result = $action->runWithParams($params); return $result; }
/** * Handles the specified request. * @param Request $request the request to be handled * @return Response the resulting response * @throws NotFoundHttpException if the requested route is invalid */ public function handleRequest($request) { list($route, $params) = $request->resolve(); try { Application::trace("Route requested: '{$route}'", __METHOD__); $this->requestedRoute = $route; $result = $this->runAction($route, $params); if ($result instanceof Response) { return $result; } else { $response = $this->getResponse(); if ($result !== null) { $response->data = $result; } return $response; } } catch (InvalidRouteException $e) { throw new NotFoundHttpException('Page not found.', $e->getCode(), $e); } }