Skip to content

Commit

Permalink
Fix controller->redirect response handling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
markstory committed Dec 31, 2016
1 parent d04f88a commit 351c253
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Controller/Controller.php
Expand Up @@ -545,17 +545,17 @@ public function redirect($url, $status = 302)

$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;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase/Controller/ControllerTest.php
Expand Up @@ -475,6 +475,7 @@ public function testRedirectByCode($code, $msg)
$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);
Expand Down Expand Up @@ -534,6 +535,7 @@ public function testRedirectBeforeRedirectListenerReturnResponse()

$result = $Controller->redirect('http://cakephp.org');
$this->assertSame($newResponse, $result);
$this->assertSame($newResponse, $Controller->response);
}

/**
Expand Down

0 comments on commit 351c253

Please sign in to comment.