From 5df2678ba9fce1728d5f6a885a166fb25cd724a2 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 19 Dec 2010 17:09:34 -0500 Subject: [PATCH] Fixing named params that were missing the : and fixing Router::reverse() so it adds in the : --- cake/libs/router.php | 21 +++++++++++++++------ cake/tests/cases/libs/router.test.php | 16 ++++++++-------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/cake/libs/router.php b/cake/libs/router.php index 506bdb2b0..f4cfa32fd 100644 --- a/cake/libs/router.php +++ b/cake/libs/router.php @@ -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; diff --git a/cake/tests/cases/libs/router.test.php b/cake/tests/cases/libs/router.test.php index e05e50149..d9a57afc8 100644 --- a/cake/tests/cases/libs/router.test.php +++ b/cake/tests/cases/libs/router.test.php @@ -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); }