Added support for controlling namedArg behavior on routes level, fixes #4451

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6686 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
the_undefined 2008-04-17 23:14:49 +00:00
parent ab3b90503f
commit 5c0b72db85
2 changed files with 28 additions and 8 deletions

View file

@ -381,12 +381,15 @@ class Router extends Object {
$url = substr($url, 0, strpos($url, '?'));
}
extract($_this->__parseExtension($url));
foreach ($_this->routes as $route) {
if (($r = $_this->matchRoute($route, $url)) !== false) {
$_this->__currentRoute[] = $route;
list($route, $regexp, $names, $defaults, $params) = $route;
$argOptions = array();
if (isset($params['named'])) {
$argOptions = array('named' => $params['named']);
unset($params['named']);
}
// remove the first element, which is the url
array_shift($r);
// hack, pre-fill the default route names
@ -413,12 +416,13 @@ class Router extends Object {
} elseif (isset($names[$key]) && empty($names[$key]) && empty($out[$names[$key]])) {
break; //leave the default values;
} else {
extract($_this->getArgs($found));
extract($_this->getArgs($found, $argOptions));
$out['pass'] = array_merge($out['pass'], $pass);
$out['named'] = $named;
}
}
if (isset($params['pass'])) {
for ($i = count($params['pass']) - 1; $i > -1; $i--) {
if (isset($out[$params['pass'][$i]])) {
@ -1200,7 +1204,7 @@ class Router extends Object {
* @param array $params
* @static
*/
function getArgs($args) {
function getArgs($args, $options = array()) {
$_this =& Router::getInstance();
$pass = $named = array();
$args = explode('/', $args);
@ -1209,9 +1213,14 @@ class Router extends Object {
continue;
}
$param = $_this->stripEscape($param);
if (strpos($param, $_this->__argSeparator)) {
$param = explode($_this->__argSeparator, $param, 2);
$named[$param[0]] = $param[1];
if ((!isset($options['named']) || !empty($options['named'])) && strpos($param, $_this->__argSeparator)) {
list($key, $val) = explode($_this->__argSeparator, $param, 2);
if (isset($options['named']) && is_array($options['named']) && !in_array($key, $options['named'])) {
$pass[] = $param;
} else {
$named[$key] = $val;
}
} else {
$pass[] = $param;
}

View file

@ -632,7 +632,7 @@ class RouterTest extends UnitTestCase {
$this->assertEqual($result, $expected);
$this->router->reload();
$this->router->connect('/:controller/:action/*', array(), array('controller' => 'some_controller'));
$this->router->connect('/:controller_action/*', array(), array('controller' => 'some_controller'));
$this->router->connect('/', array('plugin' => 'pages', 'controller' => 'pages', 'action' => 'display'));
$result = $this->router->parse('/');
$expected = array('pass' => array(), 'named' => array(), 'controller' => 'pages', 'action' => 'display', 'plugin' => 'pages');
@ -656,6 +656,17 @@ class RouterTest extends UnitTestCase {
$expected = array('pass' => array('47fc97a9-019c-41d1-a058-1fa3cbdd56cb', 'sample-post-title'), 'named' => array(), 'id' => '47fc97a9-019c-41d1-a058-1fa3cbdd56cb', 'url_title' => 'sample-post-title', 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
$this->assertEqual($result, $expected);
$this->router->reload();
$this->router->connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => false));
$result = $this->router->parse('/posts/view/foo:bar/routing:fun');
$expected = array('pass' => array('foo:bar', 'routing:fun'), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
$this->assertEqual($result, $expected);
$this->router->reload();
$this->router->connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer')));
$result = $this->router->parse('/posts/view/foo:bar/routing:fun/answer:42');
$expected = array('pass' => array('routing:fun'), 'named' => array('foo' => 'bar', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
$this->assertEqual($result, $expected);
}
function testUuidRoutes() {