diff --git a/framework/yii/base/Action.php b/framework/yii/base/Action.php index 9e0d0735ba..574fd4c97b 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 01e8112f69..5b805ad12f 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 49af5fe9cc..af9b169717 100644 --- a/framework/yii/base/Controller.php +++ b/framework/yii/base/Controller.php @@ -110,22 +110,38 @@ 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 diff --git a/framework/yii/base/InlineAction.php b/framework/yii/base/InlineAction.php index e683105f2e..75728e00fe 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 6603b28f5e..fbe6064713 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 2baef94edb..1a58ed63df 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 e4d4211eeb..01a70ffc42 100644 --- a/framework/yii/console/Controller.php +++ b/framework/yii/console/Controller.php @@ -134,6 +134,16 @@ class Controller extends \yii\base\Controller return $args; } + /** + * 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 * diff --git a/framework/yii/web/Application.php b/framework/yii/web/Application.php index 3994ec2c55..4babc2211a 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); } @@ -107,15 +106,6 @@ class Application extends \yii\base\Application return $this->getComponent('request'); } - /** - * 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 6214c54461..ee25afce55 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; /** @@ -61,6 +62,16 @@ class Controller extends \yii\base\Controller return $args; } + /** + * 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. *