Starting to try and re-factor named params to perform better and be more

explicit with how they are used.
This commit is contained in:
mark_story 2010-12-18 00:15:09 -05:00
parent de7b324444
commit c5bab54125
3 changed files with 14 additions and 5 deletions

View file

@ -263,6 +263,15 @@ class CakeRoute {
if (array_intersect_key($keyNames, $url) != $keyNames) { if (array_intersect_key($keyNames, $url) != $keyNames) {
return false; 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); $diffUnfiltered = Set::diff($url, $defaults);
$diff = array(); $diff = array();
@ -289,7 +298,7 @@ class CakeRoute {
return false; 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']); 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 //remove any pass params, they have numeric indexes, skip any params that are in the defaults

View file

@ -89,7 +89,7 @@ class Router {
* @access public * @access public
*/ */
public static $named = array( 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, 'greedy' => true,
'separator' => ':', 'separator' => ':',
'rules' => false, 'rules' => false,
@ -972,7 +972,7 @@ class Router {
if (isset(self::$named['rules'][$param])) { if (isset(self::$named['rules'][$param])) {
$rule = self::$named['rules'][$param]; $rule = self::$named['rules'][$param];
if (Router::matchNamed($param, $val, $rule, compact('controller', 'action'))) { if (Router::matchNamed($param, $val, $rule, compact('controller', 'action'))) {
$named[$param] = $val; $named[substr($param, 1)] = $val;
unset($params[$param]); unset($params[$param]);
} }
} }

View file

@ -298,13 +298,13 @@ class CakeRouteTestCase extends CakeTestCase {
Router::connectNamed(true); Router::connectNamed(true);
$route = new CakeRoute('/:controller/:action/*', array('plugin' => null)); $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'); $this->assertEqual($result, '/posts/index/page:1');
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 5)); $result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 5));
$this->assertEqual($result, '/posts/view/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'); $this->assertEqual($result, '/posts/view/5/page:1/limit:20/order:title');