Skip to content

Commit 52264cb

Browse files
committed
Fixing persistent param route exiting.
Adding test for exiting persist param routes.
1 parent 35fac16 commit 52264cb

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

cake/libs/router.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,13 @@ function url($url = null, $full = false) {
844844
$originalUrl = $url;
845845

846846
if (isset($route->params['persist'], $_this->__params[0])) {
847-
$url = array_merge(array_intersect_key($params, Set::combine($route->params['persist'], '/')), $url);
847+
foreach ($route->params['persist'] as $persistKey) {
848+
if (array_key_exists($persistKey, $_url)) {
849+
$url[$persistKey] = $_url[$persistKey];
850+
} elseif (array_key_exists($persistKey, $params)) {
851+
$url[$persistKey] = $params[$persistKey];
852+
}
853+
}
848854
}
849855
if ($match = $route->match($url)) {
850856
$output = trim($match, '/');
@@ -897,7 +903,7 @@ function url($url = null, $full = false) {
897903
break;
898904
}
899905
}
900-
$output = implode('/', $urlOut) . '/';
906+
$output = implode('/', $urlOut);
901907
}
902908

903909
if (!empty($args)) {

cake/tests/cases/libs/router.test.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -947,13 +947,29 @@ function testUrlParsing() {
947947
$result = Router::parse('/posts/view/foo:bar/routing:fun/answer:42');
948948
$expected = array('pass' => array(), 'named' => array('foo' => 'bar', 'routing' => 'fun', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
949949
$this->assertEqual($result, $expected);
950+
}
950951

952+
/**
953+
* test that the persist key works.
954+
*
955+
* @return void
956+
*/
957+
function testPersistentParameters() {
951958
Router::reload();
952-
Router::connect('/:lang/:color/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('persist' => array('lang', 'color')));
953-
Router::connect('/:lang/:color/posts/index', array('controller' => 'posts', 'action' => 'index'), array('persist' => array('lang')));
959+
Router::connect(
960+
'/:lang/:color/posts/view/*',
961+
array('controller' => 'posts', 'action' => 'view'),
962+
array('persist' => array('lang', 'color')
963+
));
964+
Router::connect(
965+
'/:lang/:color/posts/index',
966+
array('controller' => 'posts', 'action' => 'index'),
967+
array('persist' => array('lang')
968+
));
954969
Router::connect('/:lang/:color/posts/edit/*', array('controller' => 'posts', 'action' => 'index'));
955970
Router::connect('/about', array('controller' => 'pages', 'action' => 'view', 'about'));
956971
Router::parse('/en/red/posts/view/5');
972+
957973
Router::setRequestInfo(array(
958974
array('controller' => 'posts', 'action' => 'view', 'lang' => 'en', 'color' => 'red', 'form' => array(), 'url' => array(), 'plugin' => null),
959975
array('base' => '/', 'here' => '/en/red/posts/view/5', 'webroot' => '/', 'passedArgs' => array(), 'argSeparator' => ':', 'namedArgs' => array())
@@ -966,6 +982,10 @@ function testUrlParsing() {
966982
$result = Router::url(array('controller' => 'posts', 'action' => 'index', 'color' => 'blue'));
967983
$this->assertEqual($result, $expected);
968984

985+
$expected = '/posts/edit/6';
986+
$result = Router::url(array('controller' => 'posts', 'action' => 'edit', 6, 'color' => null, 'lang' => null));
987+
$this->assertEqual($result, $expected);
988+
969989
$expected = '/posts';
970990
$result = Router::url(array('controller' => 'posts', 'action' => 'index'));
971991
$this->assertEqual($result, $expected);

0 commit comments

Comments
 (0)