Skip to content

Commit

Permalink
Removing named parameter sigils.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Dec 20, 2010
1 parent d3fc29c commit b49b49a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
42 changes: 25 additions & 17 deletions cake/libs/route/cake_route.php
Expand Up @@ -284,27 +284,19 @@ public function match($url) {
return false;
}

$greedyNamed = Router::$named['greedy'];
$allowedNamedParams = Router::$named['rules'];

$named = $pass = $_query = array();

foreach ($url as $key => $value) {
// pull out named params so comparisons later on are faster.
if ($key[0] === CakeRoute::SIGIL_NAMED && ($value !== false && $value !== null)) {
$named[substr($key, 1)] = $value;
unset($url[$key]);
continue;
}

// pull out querystring params
if ($key[0] === CakeRoute::SIGIL_QUERYSTRING && ($value !== false && $value !== null)) {
$_query[substr($key, 1)] = $value;
unset($url[$key]);
continue;
}

// keys that exist in the defaults and have different values cause match failures.
$keyExists = array_key_exists($key, $defaults);
if ($keyExists && $defaults[$key] != $value) {
// keys that exist in the defaults and have different values is a match failure.
$defaultExists = array_key_exists($key, $defaults);
if ($defaultExists && $defaults[$key] != $value) {
return false;
} elseif ($defaultExists) {
continue;
}

// If the key is a routed key, its not different yet.
Expand All @@ -322,8 +314,24 @@ public function match($url) {
continue;
}

// pull out querystring params
if ($key[0] === CakeRoute::SIGIL_QUERYSTRING && ($value !== false && $value !== null)) {
$_query[substr($key, 1)] = $value;
unset($url[$key]);
continue;
}

// pull out named params if named params are greedy or a rule exists.
if (
($greedyNamed || isset($allowedNamedParams[$key])) &&
($value !== false && $value !== null)
) {
$named[$key] = $value;
continue;
}

// keys that don't exist are different.
if (!$keyExists && !empty($value)) {
if (!$defaultExists && !empty($value)) {
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions cake/libs/router.php
Expand Up @@ -914,10 +914,10 @@ protected static function _handleNoRoute($url) {
$key = $keys[$i];
if (is_numeric($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];
} else {
$named[$key] = $url[$key];
}
}

Expand Down
12 changes: 6 additions & 6 deletions cake/tests/cases/libs/route/cake_route.test.php
Expand Up @@ -310,7 +310,7 @@ function testGreedyRouteFailurePassedArg() {
*/
function testGreedyRouteFailureNamedParam() {
$route = new CakeRoute('/:controller/:action', array('plugin' => null));
$result = $route->match(array('controller' => 'posts', 'action' => 'view', ':page' => 1));
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'page' => 1));
$this->assertFalse($result);
}

Expand All @@ -334,9 +334,9 @@ function testMatchWithFalseyValues() {
*/
function testMatchWithNamedParametersAndPassedArgs() {
Router::connectNamed(true);

/*
$route = new CakeRoute('/:controller/:action/*', array('plugin' => null));
$result = $route->match(array('controller' => 'posts', 'action' => 'index', 'plugin' => null, ':page' => 1));
$result = $route->match(array('controller' => 'posts', 'action' => 'index', 'plugin' => null, 'page' => 1));
$this->assertEqual($result, '/posts/index/page:1');
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 5));
Expand All @@ -348,9 +348,9 @@ function testMatchWithNamedParametersAndPassedArgs() {
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, '0'));
$this->assertEqual($result, '/posts/view/0');
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 5, ':page' => 1, ':limit' => 20, ':order' => 'title'));
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 5, 'page' => 1, 'limit' => 20, 'order' => 'title'));
$this->assertEqual($result, '/posts/view/5/page:1/limit:20/order:title');

*/

$route = new CakeRoute('/test2/*', array('controller' => 'pages', 'action' => 'display', 2));
$result = $route->match(array('controller' => 'pages', 'action' => 'display', 1));
Expand All @@ -370,7 +370,7 @@ function testMatchWithNamedParametersAndPassedArgs() {
*/
function testNamedParamsWithNullFalse() {
$route = new CakeRoute('/:controller/:action/*');
$result = $route->match(array('controller' => 'posts', 'action' => 'index', ':page' => null, 'sort' => false));
$result = $route->match(array('controller' => 'posts', 'action' => 'index', 'page' => null, 'sort' => false));
$this->assertEquals('/posts/index/', $result);
}

Expand Down

0 comments on commit b49b49a

Please sign in to comment.