Browse Source

Fix controller->redirect response handling

Store references to the updated response. Previously controller methods
that forgot to `return $this->redirect()` would break in 3.4 as
`$this->response` was not correct.
Mark Story 9 years ago
parent
commit
351c253564
2 changed files with 5 additions and 3 deletions
  1. 3 3
      src/Controller/Controller.php
  2. 2 0
      tests/TestCase/Controller/ControllerTest.php

+ 3 - 3
src/Controller/Controller.php

@@ -545,17 +545,17 @@ class Controller implements EventListenerInterface, EventDispatcherInterface
 
         $event = $this->dispatchEvent('Controller.beforeRedirect', [$url, $response]);
         if ($event->result() instanceof Response) {
-            return $event->result();
+            return $this->response = $event->result();
         }
         if ($event->isStopped()) {
             return null;
         }
 
         if (!$response->location()) {
-            $response->location(Router::url($url, true));
+            $response = $response->withLocation(Router::url($url, true));
         }
 
-        return $response;
+        return $this->response = $response;
     }
 
     /**

+ 2 - 0
tests/TestCase/Controller/ControllerTest.php

@@ -475,6 +475,7 @@ class ControllerTest extends TestCase
         $Controller = new Controller(null, new Response());
 
         $response = $Controller->redirect('http://cakephp.org', (int)$code);
+        $this->assertSame($response, $Controller->response);
         $this->assertEquals($code, $response->statusCode());
         $this->assertEquals('http://cakephp.org', $response->header()['Location']);
         $this->assertFalse($Controller->autoRender);
@@ -534,6 +535,7 @@ class ControllerTest extends TestCase
 
         $result = $Controller->redirect('http://cakephp.org');
         $this->assertSame($newResponse, $result);
+        $this->assertSame($newResponse, $Controller->response);
     }
 
     /**