From 5c0b72db857b81246b0ee1413e1d7a13c5aef7bf Mon Sep 17 00:00:00 2001 From: the_undefined Date: Thu, 17 Apr 2008 23:14:49 +0000 Subject: [PATCH] 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 --- cake/libs/router.php | 23 ++++++++++++++++------- cake/tests/cases/libs/router.test.php | 13 ++++++++++++- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/cake/libs/router.php b/cake/libs/router.php index 433a0ab09..fb6d3ce1b 100644 --- a/cake/libs/router.php +++ b/cake/libs/router.php @@ -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; } diff --git a/cake/tests/cases/libs/router.test.php b/cake/tests/cases/libs/router.test.php index cff65694e..45543958d 100644 --- a/cake/tests/cases/libs/router.test.php +++ b/cake/tests/cases/libs/router.test.php @@ -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() {