Skip to content

Commit

Permalink
Fixing named params that were missing the : and fixing Router::revers…
Browse files Browse the repository at this point in the history
…e() so it adds in the :
  • Loading branch information
markstory committed Dec 19, 2010
1 parent c83a470 commit 5df2678
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
21 changes: 15 additions & 6 deletions cake/libs/router.php
Expand Up @@ -897,7 +897,7 @@ public static function url($url = null, $full = false) {
* @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
Expand All @@ -908,10 +908,13 @@ protected static function _handleNoRoute($url) {

// 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];
}
}

Expand All @@ -923,7 +926,7 @@ protected static function _handleNoRoute($url) {
}
}

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;
}

Expand All @@ -947,7 +950,6 @@ protected static function _handleNoRoute($url) {

if (!empty($named)) {
foreach ($named as $name => $value) {
$name = trim($name, ':');
if (is_array($value)) {
$flattend = Set::flatten($value, '][');
foreach ($flattend as $namedKey => $namedValue) {
Expand All @@ -958,6 +960,9 @@ protected static function _handleNoRoute($url) {
}
}
}
if (!empty($query)) {
$output .= Router::queryString($query);
}
return $output;
}

Expand Down Expand Up @@ -1070,6 +1075,10 @@ public static function reverse($params) {
$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;
Expand Down
16 changes: 8 additions & 8 deletions cake/tests/cases/libs/router.test.php
Expand Up @@ -630,7 +630,7 @@ function testUrlGenerationWithAdminPrefix() {
$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);

Expand Down Expand Up @@ -1546,12 +1546,12 @@ function testUrlGenerationWithLegacyPrefixes() {
$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);
}
Expand Down Expand Up @@ -1610,12 +1610,12 @@ function testUrlGenerationWithAutoPrefixes() {
$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);
}
Expand Down Expand Up @@ -1721,7 +1721,7 @@ function testRemoveBase() {
$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);
}

Expand Down Expand Up @@ -1908,7 +1908,7 @@ function testParsingWithPrefixes() {
$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);

Expand Down Expand Up @@ -2083,7 +2083,7 @@ function testRegexRouteMatching() {
$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);
}

Expand Down

0 comments on commit 5df2678

Please sign in to comment.