Skip to content

Commit

Permalink
RedirectRoute class does not honor persist as array with custom route…
Browse files Browse the repository at this point in the history
… elements.

Custom route elements like '/:lang/etc' should be persisted by redirect
routes as they are in standard routes.

Refs #GH-1531
  • Loading branch information
joostdekeijzer authored and markstory committed Aug 17, 2013
1 parent 8428928 commit 7fe2395
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/Cake/Routing/Route/RedirectRoute.php
Expand Up @@ -80,6 +80,13 @@ public function parse($url) {
}
if (isset($this->options['persist']) && is_array($redirect)) {
$redirect += array('named' => $params['named'], 'pass' => $params['pass'], 'url' => array());
if (is_array($this->options['persist'])) {
foreach ($this->options['persist'] as $elem) {
if (isset($params[$elem])) {
$redirect[$elem] = $params[$elem];
}
}
}
$redirect = Router::reverse($redirect);
}
$status = 301;
Expand Down
16 changes: 16 additions & 0 deletions lib/Cake/Test/Case/Routing/Route/RedirectRouteTest.php
Expand Up @@ -103,6 +103,22 @@ public function testParsing() {
$result = $route->parse('/my_controllers/do_something/passme/named:param');
$header = $route->response->header();
$this->assertEquals(Router::url('/tags/add', true), $header['Location']);

$route = new RedirectRoute('/:lang/my_controllers', array('controller' => 'tags', 'action' => 'add'), array('lang' => '(nl|en)', 'persist' => array('lang')));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/nl/my_controllers/');
$header = $route->response->header();
$this->assertEquals(Router::url('/tags/add/lang:nl', true), $header['Location']);

Router::$routes = array(); // reset default routes
Router::connect('/:lang/preferred_controllers', array('controller' => 'tags', 'action' => 'add'), array('lang' => '(nl|en)', 'persist' => array('lang')));
$route = new RedirectRoute('/:lang/my_controllers', array('controller' => 'tags', 'action' => 'add'), array('lang' => '(nl|en)', 'persist' => array('lang')));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/nl/my_controllers/');
$header = $route->response->header();
$this->assertEquals(Router::url('/nl/preferred_controllers', true), $header['Location']);
}

}

0 comments on commit 7fe2395

Please sign in to comment.