Skip to content

Commit

Permalink
Allow RequestHandlerComponent::beforeRedirect to be disabled.
Browse files Browse the repository at this point in the history
The beforeRedirect behavior has surprising behavior and is not something
that we'd like to keep around long term. Deprecate the functionality,
and provide a config flag for disabling it now.

Refs #9525
Refs #9505
  • Loading branch information
markstory committed Sep 28, 2016
1 parent ca96417 commit 28a2fed
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Controller/Component/RequestHandlerComponent.php
Expand Up @@ -79,13 +79,16 @@ class RequestHandlerComponent extends Component
* json, xml, and ajax will be mapped. Defining any types will omit the defaults.
* - `inputTypeMap` - A mapping between types and deserializers for request bodies.
* If undefined json & xml will be mapped. Defining any types will omit the defaults.
* - `enableBeforeRedirect` - Set to false to disable the `beforeRedirect` callback. The
* `beforeRedirect` functionality has been deprecated.
*
* @var array
*/
protected $_defaultConfig = [
'checkHttpCache' => true,
'viewClassMap' => [],
'inputTypeMap' => []
'inputTypeMap' => [],
'enableBeforeRedirect' => true
];

/**
Expand Down Expand Up @@ -252,9 +255,14 @@ public function convertXml($xml)
* @param string|array $url A string or array containing the redirect location
* @param \Cake\Network\Response $response The response object.
* @return \Cake\Network\Response|null The response object if the redirect is caught.
* @deprecated 3.3.5 This functionality will be removed in 4.0.0. You can disable this function
* now by setting the `enableBeforeRedirect` config option to false.
*/
public function beforeRedirect(Event $event, $url, Response $response)
{
if ($this->config('enableBeforeRedirect') == false) {
return null;
}
$request = $this->request;
if (!$request->is('ajax')) {
return null;
Expand Down
Expand Up @@ -590,6 +590,25 @@ public function testStartupCustomTypeProcess()
error_reporting($restore);
}

/**
* test beforeRedirect when disabled.
*
* @return void
* @triggers Controller.startup $this->Controller
*/
public function testBeforeRedirectDisabled()
{
Configure::write('App.namespace', 'TestApp');
Router::connect('/:controller/:action');
$this->Controller->request->env('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');

$event = new Event('Controller.startup', $this->Controller);
$this->RequestHandler->initialize([]);
$this->RequestHandler->config('enableBeforeRedirect', false);
$this->RequestHandler->startup($event);
$this->assertNull($this->RequestHandler->beforeRedirect($event, '/posts/index', $this->Controller->response));
}

/**
* testNonAjaxRedirect method
*
Expand Down

0 comments on commit 28a2fed

Please sign in to comment.