Fixing named params that were missing the : and fixing Router::reverse() so it adds in the :

This commit is contained in:
mark_story 2010-12-19 17:09:34 -05:00
parent c83a4703a3
commit 5df2678ba9
2 changed files with 23 additions and 14 deletions

View file

@ -897,7 +897,7 @@ class Router {
* @see Router::url()
*/
protected static function _handleNoRoute($url) {
$named = $args = array();
$named = $args = $query = array();
$skip = array_merge(
array('bare', 'action', 'controller', 'plugin', 'prefix'),
self::$_prefixes
@ -908,10 +908,13 @@ class Router {
// Remove this once parsed URL parameters can be inserted into 'pass'
for ($i = 0; $i < $count; $i++) {
$key = $keys[$i];
if (is_numeric($keys[$i])) {
$args[] = $url[$keys[$i]];
} else {
$named[$keys[$i]] = $url[$keys[$i]];
$args[] = $url[$key];
} elseif ($key[0] === CakeRoute::SIGIL_NAMED) {
$named[substr($key, 1)] = $url[$key];
} elseif ($key[0] === CakeRoute::SIGIL_QUERYSTRING) {
$query[substr($key, 1)] = $url[$key];
}
}
@ -923,7 +926,7 @@ class Router {
}
}
if (empty($named) && empty($args) && (!isset($url['action']) || $url['action'] === 'index')) {
if (empty($named) && empty($args) && empty($query) && (!isset($url['action']) || $url['action'] === 'index')) {
$url['action'] = null;
}
@ -947,7 +950,6 @@ class Router {
if (!empty($named)) {
foreach ($named as $name => $value) {
$name = trim($name, ':');
if (is_array($value)) {
$flattend = Set::flatten($value, '][');
foreach ($flattend as $namedKey => $namedValue) {
@ -958,6 +960,9 @@ class Router {
}
}
}
if (!empty($query)) {
$output .= Router::queryString($query);
}
return $output;
}
@ -1070,6 +1075,10 @@ class Router {
$params['pass'], $params['named'], $params['paging'], $params['models'], $params['url'], $url['url'],
$params['autoRender'], $params['bare'], $params['requested'], $params['return']
);
foreach ($named as $key => $value) {
$named[CakeRoute::SIGIL_NAMED . $key] = $value;
unset($named[$key]);
}
$params = array_merge($params, $pass, $named);
if (!empty($url)) {
$params['?'] = $url;

View file

@ -630,7 +630,7 @@ class RouterTest extends CakeTestCase {
$request->webroot = '/';
Router::setRequestInfo($request);
$result = Router::url(array('page' => 2));
$result = Router::url(array(':page' => 2));
$expected = '/admin/registrations/index/page:2';
$this->assertEqual($result, $expected);
@ -1546,12 +1546,12 @@ class RouterTest extends CakeTestCase {
$expected = '/protected/others/edit/1';
$this->assertEqual($result, $expected);
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, 'page' => 1));
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, ':page' => 1));
$expected = '/protected/others/edit/1/page:1';
$this->assertEqual($result, $expected);
Router::connectNamed(array('random'));
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, 'random' => 'my-value'));
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, ':random' => 'my-value'));
$expected = '/protected/others/edit/1/random:my-value';
$this->assertEqual($result, $expected);
}
@ -1610,12 +1610,12 @@ class RouterTest extends CakeTestCase {
$expected = '/protected/others/edit/1';
$this->assertEqual($result, $expected);
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, 'page' => 1));
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, ':page' => 1));
$expected = '/protected/others/edit/1/page:1';
$this->assertEqual($result, $expected);
Router::connectNamed(array('random'));
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, 'random' => 'my-value'));
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, ':random' => 'my-value'));
$expected = '/protected/others/edit/1/random:my-value';
$this->assertEqual($result, $expected);
}
@ -1721,7 +1721,7 @@ class RouterTest extends CakeTestCase {
$this->assertEqual($result, $expected);
$result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action', 'base' => true));
$expected = '/base/my_controller/my_action/base:1';
$expected = '/base/my_controller/my_action';
$this->assertEqual($result, $expected);
}
@ -1908,7 +1908,7 @@ class RouterTest extends CakeTestCase {
$expected = array('pass' => array(), 'named' => array(), 'prefix' => 'members', 'plugin' => null, 'controller' => 'posts', 'action' => 'index', 'members' => true);
$this->assertEqual($result, $expected);
$result = Router::url(array('members' => true, 'controller' => 'posts', 'action' =>'index', 'page' => 2));
$result = Router::url(array('members' => true, 'controller' => 'posts', 'action' =>'index', ':page' => 2));
$expected = '/base/members/posts/index/page:2';
$this->assertEqual($result, $expected);
@ -2083,7 +2083,7 @@ class RouterTest extends CakeTestCase {
$this->assertEqual($result, $expected);
$result = Router::url(array('action' => 'test_another_action', 'locale' => 'badness'));
$expected = '/test/test_another_action/locale:badness';
$expected = '/test/test_another_action';
$this->assertEqual($result, $expected);
}