diff --git a/framework/yii/base/Action.php b/framework/yii/base/Action.php index 9e0d073..574fd4c 100644 --- a/framework/yii/base/Action.php +++ b/framework/yii/base/Action.php @@ -41,17 +41,27 @@ class Action extends Component * @var Controller the controller that owns this action */ public $controller; - + /** + * @var Response + */ private $_response; + + /** + * @return Response|\yii\console\Response|\yii\web\Response + */ public function getResponse() { if ($this->_response === null) { - $this->_response = Yii::$app->createResponse(); + // TODO use applications response factory here + //$this->_response = new Response(); } return $this->_response; } + /** + * @param Response $response + */ public function setResponse($response) { $this->_response = $response; @@ -92,8 +102,6 @@ class Action extends Component throw new InvalidConfigException(get_class($this) . ' must define a "run()" method.'); } $args = $this->controller->bindActionParams($this, $params); - $response = $this->getResponse(); - $response->result = call_user_func_array(array($this, 'run'), $args); - return $response; + return call_user_func_array(array($this, 'run'), $args); } } diff --git a/framework/yii/base/Application.php b/framework/yii/base/Application.php index 01e8112..5b805ad 100644 --- a/framework/yii/base/Application.php +++ b/framework/yii/base/Application.php @@ -155,8 +155,6 @@ abstract class Application extends Module */ abstract public function handleRequest($request); - public abstract function createResponse(); - private $_runtimePath; /** diff --git a/framework/yii/base/Controller.php b/framework/yii/base/Controller.php index 49af5fe..af9b169 100644 --- a/framework/yii/base/Controller.php +++ b/framework/yii/base/Controller.php @@ -110,23 +110,39 @@ class Controller extends Component if ($action !== null) { $oldAction = $this->action; $this->action = $action; - $result = null; - // TODO beforeAction may also create a response somehow. if ($this->module->beforeAction($action)) { if ($this->beforeAction($action)) { $result = $action->runWithParams($params); + if ($result !== null) { + $this->handleActionResult($result, $action); + } $this->afterAction($action); } $this->module->afterAction($action); } $this->action = $oldAction; - return $result; + return $action->getResponse(); } else { throw new InvalidRouteException('Unable to resolve the request: ' . $this->getUniqueId() . '/' . $id); } } /** + * Handles the return value of an action + * @param mixed $result + * @param Action $action + */ + protected abstract function handleActionResult(&$result, $action); + + /** + * @return Response the response object of the current action. null if no action is running + */ + public function getResponse() + { + return $this->action !== null ? $this->action->getResponse() : null; + } + + /** * Runs a request specified in terms of a route. * The route can be either an ID of an action within this controller or a complete route consisting * of module IDs, controller ID and action ID. If the route starts with a slash '/', the parsing of diff --git a/framework/yii/base/InlineAction.php b/framework/yii/base/InlineAction.php index e683105..75728e0 100644 --- a/framework/yii/base/InlineAction.php +++ b/framework/yii/base/InlineAction.php @@ -44,8 +44,6 @@ class InlineAction extends Action public function runWithParams($params) { $args = $this->controller->bindActionParams($this, $params); - $response = $this->getResponse(); - $response->result = call_user_func_array(array($this->controller, $this->actionMethod), $args); - return $response; + return call_user_func_array(array($this->controller, $this->actionMethod), $args); } } diff --git a/framework/yii/base/Module.php b/framework/yii/base/Module.php index 6603b28..fbe6064 100644 --- a/framework/yii/base/Module.php +++ b/framework/yii/base/Module.php @@ -571,7 +571,7 @@ abstract class Module extends Component * If the route is empty, the method will use [[defaultRoute]]. * @param string $route the route that specifies the action. * @param array $params the parameters to be passed to the action - * @return mixed the result of the action. + * @return Response the resulting response of the action. * @throws InvalidRouteException if the requested route cannot be resolved into an action successfully */ public function runAction($route, $params = array()) @@ -582,9 +582,9 @@ abstract class Module extends Component list($controller, $actionID) = $parts; $oldController = Yii::$app->controller; Yii::$app->controller = $controller; - $result = $controller->runAction($actionID, $params); + $response = $controller->runAction($actionID, $params); Yii::$app->controller = $oldController; - return $result; + return $response; } else { throw new InvalidRouteException('Unable to resolve the request "' . trim($this->getUniqueId() . '/' . $route, '/') . '".'); } diff --git a/framework/yii/console/Application.php b/framework/yii/console/Application.php index 2baef94..1a58ed6 100644 --- a/framework/yii/console/Application.php +++ b/framework/yii/console/Application.php @@ -88,39 +88,13 @@ class Application extends \yii\base\Application * Handles the specified request. * @param Request $request the request to be handled * @return Response the resulting response + * @throws Exception if the route is invalid */ public function handleRequest($request) { list ($route, $params) = $request->resolve(); - $result = $this->runAction($route, $params); - $response = $this->getResponse(); - $response->exitStatus = (int)$result; - return $response; - } - - /** - * Returns the response component. - * @return Response the response component - */ - public function createResponse() - { - return new Response(); - } - - /** - * Runs a controller action specified by a route. - * 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]]. - * @param string $route the route that specifies the action. - * @param array $params the parameters to be passed to the action - * @return integer the status code returned by the action execution. 0 means normal, and other values mean abnormal. - * @throws Exception if the route is invalid - */ - public function runAction($route, $params = array()) - { try { - return parent::runAction($route, $params); + return $this->runAction($route, $params); } catch (InvalidRouteException $e) { throw new Exception(\Yii::t('yii', 'Unknown command "{command}".', array('{command}' => $route)), 0, $e); } @@ -152,9 +126,6 @@ class Application extends \yii\base\Application 'request' => array( 'class' => 'yii\console\Request', ), - 'response' => array( - 'class' => 'yii\console\Response', - ), )); } } diff --git a/framework/yii/console/Controller.php b/framework/yii/console/Controller.php index e4d4211..01a70ff 100644 --- a/framework/yii/console/Controller.php +++ b/framework/yii/console/Controller.php @@ -135,6 +135,16 @@ class Controller extends \yii\base\Controller } /** + * Handles the return value of an action + * @param mixed $result + * @param Action $action + */ + protected function handleActionResult(&$result, $action) + { + $action->getResponse()->exitStatus = (int)$result; + } + + /** * Formats a string with ANSI codes * * You may pass additional parameters using the constants defined in [[yii\helpers\base\Console]]. diff --git a/framework/yii/web/Application.php b/framework/yii/web/Application.php index 3994ec2..4babc22 100644 --- a/framework/yii/web/Application.php +++ b/framework/yii/web/Application.php @@ -65,8 +65,7 @@ class Application extends \yii\base\Application $params = array_splice($this->catchAll, 1); } try { - $response = $this->runAction($route, $params); - return $response; + return $this->runAction($route, $params); } catch (InvalidRouteException $e) { throw new HttpException(404, $e->getMessage(), $e->getCode(), $e); } @@ -108,15 +107,6 @@ class Application extends \yii\base\Application } /** - * Returns the response component. - * @return Response the response component - */ - public function createResponse() - { - return new Response(); - } - - /** * Returns the session component. * @return Session the session component */ @@ -154,9 +144,6 @@ class Application extends \yii\base\Application 'request' => array( 'class' => 'yii\web\Request', ), - 'response' => array( - 'class' => 'yii\web\Response', - ), 'session' => array( 'class' => 'yii\web\Session', ), diff --git a/framework/yii/web/Controller.php b/framework/yii/web/Controller.php index 6214c54..ee25afc 100644 --- a/framework/yii/web/Controller.php +++ b/framework/yii/web/Controller.php @@ -8,6 +8,7 @@ namespace yii\web; use Yii; +use yii\base\Action; use yii\base\InlineAction; /** @@ -62,6 +63,16 @@ class Controller extends \yii\base\Controller } /** + * Handles the return value of an action + * @param mixed $result + * @param Action $action + */ + protected function handleActionResult(&$result, $action) + { + $action->getResponse()->setContent($result); + } + + /** * Creates a URL using the given route and parameters. * * This method enhances [[UrlManager::createUrl()]] by supporting relative routes.