Skip to content
Permalink
Browse files

Ensure status code is set.

Set the status code into the response before firing the beforeRedirect
event. This allows for fewer arguments in each event listener but still
allows listeners to read and modify the status code.

Remove redirect with string status codes. The documentation and simplest
use is with integers. String names for status code are more verbose and
require additional logic to handle. If necessary constants can be added
for the most common status codes.
  • Loading branch information...
markstory committed Jul 20, 2013
1 parent d64b7ed commit 7e1e36c515d343f0735f8744709896374f849911
@@ -264,11 +264,6 @@ public function beforeRedirect(Event $event, $url, $response) {
if (is_array($url)) {
$url = Router::url($url + array('base' => false));
}
if (!empty($status)) {
$statusCode = $response->httpCodes($status);
$code = key($statusCode);
$response->statusCode($code);
}
$controller = $event->subject();
$response->body($controller->requestAction($url, array('return', 'bare' => false)));
$response->send();
@@ -28,8 +28,8 @@
use Cake\Routing\Router;
use Cake\Utility\ClassRegistry;
use Cake\Utility\Inflector;
use Cake\Utility\MergeVariablesTrait;
use Cake\Utility\ObjectCollection;
use Cake\Utility\MergeVariablesTrait;
use Cake\Utility\ViewVarsTrait;
use Cake\View\View;
@@ -747,10 +747,11 @@ public function loadModel($modelClass = null, $id = null) {
public function redirect($url, $status = null, $exit = true) {
$this->autoRender = false;
if (is_array($status)) {
extract($status, EXTR_OVERWRITE);
}
$response = $this->response;
if ($status && $response->statusCode() === 200) {
$response->statusCode($status);
}
$event = new Event('Controller.beforeRedirect', $this, [$response, $url, $status]);
$this->getEventManager()->dispatch($event);
if ($event->isStopped()) {
@@ -761,17 +762,6 @@ public function redirect($url, $status = null, $exit = true) {
$response->location(Router::url($url, true));
}
if (is_string($status)) {
$codes = array_flip($response->httpCodes());
if (isset($codes[$status])) {
$status = $codes[$status];
}
}
if ($status && $response->statusCode() === 200) {
$response->statusCode($status);
}
if ($exit) {
$response->send();
$this->_stop();
@@ -855,30 +855,6 @@ public function testBeforeRedirectCallbackWithArrayUrl() {
$this->assertEquals('one: first two: second', $result);
}
/**
* assure that beforeRedirect with a status code will correctly set the status header
*
* @return void
*/
public function testBeforeRedirectCallingHeader() {
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
$event = new Event('Controller.beforeRender', $this->Controller);
$controller = $this->getMock('Cake\Controller\Controller', array('header'));
$RequestHandler = $this->getMock('Cake\Controller\Component\RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
$RequestHandler->response = $this->getMock('Cake\Network\Response', array('_sendHeader','statusCode'));
$RequestHandler->request = $this->getMock('Cake\Network\Request');
$RequestHandler->request->expects($this->once())->method('is')
->with('ajax')
->will($this->returnValue(true));
$RequestHandler->response->expects($this->once())->method('statusCode')->with(403);
ob_start();
$RequestHandler->beforeRedirect($event, 'request_handler_test/param_method/first/second', $controller->response);
ob_get_clean();
}
/**
* @expectedException Cake\Error\Exception
* @return void
@@ -944,6 +920,6 @@ public function testCheckNotModifiedNoInfo() {
$RequestHandler = $this->getMock('Cake\Controller\Component\RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
$RequestHandler->response = $this->getMock('Cake\Network\Response', array('notModified'));
$RequestHandler->response->expects($this->never())->method('notModified');
$this->assertNull($RequestHandler->beforeRender($event));
$this->assertNull($RequestHandler->beforeRender($event, '', $RequestHandler->response));
}
}
@@ -561,23 +561,6 @@ public function testRedirectByCode($code, $msg) {
$this->assertFalse($Controller->autoRender);
}
/**
* test redirecting by message
*
* @dataProvider statusCodeProvider
* @return void
*/
public function testRedirectByMessage($code, $msg) {
$Controller = new Controller(null);
$Controller->response = new Response();
$Controller->redirect('http://cakephp.org', $msg, false);
$this->assertEquals($code, $Controller->response->statusCode());
$this->assertEquals('http://cakephp.org', $Controller->response->header()['Location']);
$this->assertFalse($Controller->autoRender);
$this->assertFalse($Controller->autoRender);
}
/**
* test that beforeRedirect callbacks can set the URL that is being redirected to.
*

0 comments on commit 7e1e36c

Please sign in to comment.
You can’t perform that action at this time.