Skip to content

Commit

Permalink
Starting to try and re-factor named params to perform better and be more
Browse files Browse the repository at this point in the history
explicit with how they are used.
  • Loading branch information
markstory committed Dec 18, 2010
1 parent de7b324 commit c5bab54
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
11 changes: 10 additions & 1 deletion cake/libs/route/cake_route.php
Expand Up @@ -263,6 +263,15 @@ public function match($url) {
if (array_intersect_key($keyNames, $url) != $keyNames) {
return false;
}

//pull out named params so comparisons later on are faster.
$named = array();
foreach ($url as $key => $value) {
if ($key[0] === ':') {
$named[$key] = $value;
unset($url[$key]);
}
}

$diffUnfiltered = Set::diff($url, $defaults);
$diff = array();
Expand All @@ -289,7 +298,7 @@ public function match($url) {
return false;
}

$passedArgsAndParams = array_diff_key($diff, $filteredDefaults, $keyNames);
$passedArgsAndParams = array_diff_key($diff, $filteredDefaults, $keyNames) + $named;
list($named, $params) = Router::getNamedElements($passedArgsAndParams, $url['controller'], $url['action']);

//remove any pass params, they have numeric indexes, skip any params that are in the defaults
Expand Down
4 changes: 2 additions & 2 deletions cake/libs/router.php
Expand Up @@ -89,7 +89,7 @@ class Router {
* @access public
*/
public static $named = array(
'default' => array('page', 'fields', 'order', 'limit', 'recursive', 'sort', 'direction', 'step'),
'default' => array(':page', ':fields', ':order', ':limit', ':recursive', ':sort', ':direction', ':step'),
'greedy' => true,
'separator' => ':',
'rules' => false,
Expand Down Expand Up @@ -972,7 +972,7 @@ public static function getNamedElements($params, $controller = null, $action = n
if (isset(self::$named['rules'][$param])) {
$rule = self::$named['rules'][$param];
if (Router::matchNamed($param, $val, $rule, compact('controller', 'action'))) {
$named[$param] = $val;
$named[substr($param, 1)] = $val;
unset($params[$param]);
}
}
Expand Down
4 changes: 2 additions & 2 deletions cake/tests/cases/libs/route/cake_route.test.php
Expand Up @@ -298,13 +298,13 @@ 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));
$this->assertEqual($result, '/posts/view/5');

$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');


Expand Down

0 comments on commit c5bab54

Please sign in to comment.