Browse Source

Made Response instance the only valid return type for controller action.

ADmad 12 years ago
parent
commit
08477e8334

+ 15 - 15
src/Routing/Dispatcher.php

@@ -163,7 +163,7 @@ class Dispatcher implements EventListener {
 			));
 		}
 
-		$response = $this->_invoke($controller, $response);
+		$response = $this->_invoke($controller);
 		if (isset($request->params['return'])) {
 			return $response->body();
 		}
@@ -175,31 +175,31 @@ class Dispatcher implements EventListener {
 
 /**
  * Initializes the components and models a controller will be using.
- * Triggers the controller action, and invokes the rendering if Controller::$autoRender is true and echo's the output.
- * Otherwise the return value of the controller action are returned.
+ * Triggers the controller action and invokes the rendering if Controller::$autoRender
+ * is true. If a response object is returned by controller action that is returned
+ * else controller's $response property is returned.
  *
  * @param Controller $controller Controller to invoke
- * @param \Cake\Network\Response $response The response object to receive the output
- * @return \Cake\Network\Response the resulting response object
+ * @return \Cake\Network\Response The resulting response object
+ * @throws \Cake\Error\Exception If data returned by controller action is not an
+ *   instance of Response
  */
-	protected function _invoke(Controller $controller, Response $response) {
+	protected function _invoke(Controller $controller) {
 		$controller->constructClasses();
 		$controller->startupProcess();
 
-		$render = true;
-		$result = $controller->invokeAction();
-		if ($result instanceof Response) {
-			$render = false;
-			$response = $result;
+		$response = $controller->invokeAction();
+		if ($response !== null && !($response instanceof Response)) {
+			throw new Error\Exception('Controller action can only return an instance of Response');
 		}
 
-		if ($render && $controller->autoRender) {
+		if (!$response && $controller->autoRender) {
 			$response = $controller->render();
-		} elseif (!($result instanceof CakeResponse) && $response->body() === null) {
-			$response->body($result);
+		} elseif (!$response) {
+			$response = $controller->response;
 		}
-		$controller->shutdownProcess();
 
+		$controller->shutdownProcess();
 		return $response;
 	}
 

+ 3 - 4
tests/TestCase/Routing/DispatcherTest.php

@@ -58,12 +58,11 @@ class TestDispatcher extends Dispatcher {
  * invoke method
  *
  * @param \Cake\Controller\Controller $controller
- * @param \Cake\Network\Response $response
- * @return void
+ * @return \Cake\Network\Response $response
  */
-	protected function _invoke(Controller $controller, Response $response) {
+	protected function _invoke(Controller $controller) {
 		$this->controller = $controller;
-		return parent::_invoke($controller, $response);
+		return parent::_invoke($controller);
 	}
 
 /**

+ 14 - 12
tests/test_app/TestApp/Controller/RequestActionController.php

@@ -33,10 +33,11 @@ class RequestActionController extends AppController {
 /**
  * test_request_action method
  *
- * @return string
+ * @return \Cake\Network\Response
  */
 	public function test_request_action() {
-		return 'This is a test';
+		$this->response->body('This is a test');
+		return $this->response;
 	}
 
 /**
@@ -44,39 +45,40 @@ class RequestActionController extends AppController {
  *
  * @param mixed $id
  * @param mixed $other
- * @access public
- * @return string
+ * @return \Cake\Network\Response
  */
 	public function another_ra_test($id, $other) {
-		return $id + $other;
+		$this->response->body($id + $other);
+		return $this->response;
 	}
 
 /**
  * normal_request_action method
  *
- * @return string
+ * @return \Cake\Network\Response
  */
 	public function normal_request_action() {
-		return 'Hello World';
+		$this->response->body('Hello World');
+		return $this->response;
 	}
 
 /**
- * returns $this->here
+ * returns $this->here as body
  *
- * @return string
+ * @return \Cake\Network\Response
  */
 	public function return_here() {
-		return $this->here;
+		$this->response->body($this->here);
+		return $this->response;
 	}
 
 /**
  * paginate_request_action method
  *
- * @return boolean
+ * @return void
  */
 	public function paginate_request_action() {
 		$data = $this->paginate();
-		return true;
 	}
 
 /**

+ 0 - 2
tests/test_app/TestApp/Controller/SomePostsController.php

@@ -57,7 +57,6 @@ class SomePostsController extends Controller {
  * @return void
  */
 	public function index() {
-		return true;
 	}
 
 /**
@@ -66,7 +65,6 @@ class SomePostsController extends Controller {
  * @return void
  */
 	public function change() {
-		return true;
 	}
 
 }

+ 0 - 2
tests/test_app/TestApp/Controller/TestDispatchPagesController.php

@@ -34,7 +34,6 @@ class TestDispatchPagesController extends Controller {
  * @return void
  */
 	public function admin_index() {
-		return true;
 	}
 
 /**
@@ -43,7 +42,6 @@ class TestDispatchPagesController extends Controller {
  * @return void
  */
 	public function camelCased() {
-		return true;
 	}
 
 }