From 766703f15f6930a607fa5be4699a865725907ed8 Mon Sep 17 00:00:00 2001 From: nate Date: Tue, 12 Feb 2008 14:11:33 +0000 Subject: [PATCH] Fixing default route mappings, refactoring route generation, fixes #4109 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6456 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/router.php | 57 +++++++++++++-------------- cake/tests/cases/libs/router.test.php | 8 ++++ 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/cake/libs/router.php b/cake/libs/router.php index 2f0619a70..f2621fb1d 100644 --- a/cake/libs/router.php +++ b/cake/libs/router.php @@ -206,7 +206,7 @@ class Router extends Object { function connect($route, $default = array(), $params = array()) { $_this =& Router::getInstance(); $admin = Configure::read('Routing.admin'); - $default = array_merge(array('plugin' => null, 'controller' => null, 'action' => null), $default); + $default = array_merge(array('action' => null), $default); if (!empty($default) && empty($default['action'])) { $default['action'] = 'index'; @@ -221,8 +221,8 @@ class Router extends Object { $_this->__prefixes = array_unique($_this->__prefixes); } - if ($route = $_this->writeRoute($route, $default, $params)) { - $_this->routes[] = $route; + if (list($pattern, $names) = $_this->writeRoute($route, $default, $params)) { + $_this->routes[] = array($route, $pattern, $names, array_merge(array('plugin' => null, 'controller' => null), $default), $params); } return $_this->routes; } @@ -293,32 +293,31 @@ class Router extends Object { */ function writeRoute($route, $default, $params) { if (empty($route) || ($route == '/')) { - return array($route, '/^[\/]*$/', array(), $default, array()); - } else { - $names = array(); - $elements = Set::filter(array_map('trim', explode('/', $route))); - - foreach ($elements as $element) { - $q = null; - - if (preg_match('/^:(.+)$/', $element, $r)) { - if (isset($params[$r[1]])) { - if (array_key_exists($r[1], $default) && $r[1] != 'plugin') { - $q = '?'; - } - $parsed[] = '(?:\/(' . $params[$r[1]] . ')' . $q . ')' . $q; - } else { - $parsed[] = '(?:\/([^\/]+))?'; - } - $names[] = $r[1]; - } elseif (preg_match('/^\*$/', $element, $r)) { - $parsed[] = '(?:\/(.*))?'; - } else { - $parsed[] = '/' . $element; - } - } - return array($route, '#^' . join('', $parsed) . '[\/]*$#', $names, $default, $params); + return array('/^[\/]*$/', array()); } + $names = array(); + $elements = Set::filter(array_map('trim', explode('/', $route))); + + foreach ($elements as $element) { + $q = null; + + if (preg_match('/^:(.+)$/', $element, $r)) { + if (isset($params[$r[1]])) { + if (array_key_exists($r[1], $default) && $r[1] != 'plugin') { + $q = '?'; + } + $parsed[] = '(?:\/(' . $params[$r[1]] . ')' . $q . ')' . $q; + } else { + $parsed[] = '(?:\/([^\/]+))?'; + } + $names[] = $r[1]; + } elseif (preg_match('/^\*$/', $element, $r)) { + $parsed[] = '(?:\/(.*))?'; + } else { + $parsed[] = '/' . $element; + } + } + return array('#^' . join('', $parsed) . '[\/]*$#', $names); } /** * Returns the list of prefixes used in connected routes @@ -343,7 +342,7 @@ class Router extends Object { function parse($url) { $_this =& Router::getInstance(); $_this->__connectDefaultRoutes(); - $out = array('pass' => array(), 'named'=>array()); + $out = array('pass' => array(), 'named' => array()); $r = $ext = null; if (ini_get('magic_quotes_gpc') == 1) { diff --git a/cake/tests/cases/libs/router.test.php b/cake/tests/cases/libs/router.test.php index 30ec97edf..fc1a54c65 100644 --- a/cake/tests/cases/libs/router.test.php +++ b/cake/tests/cases/libs/router.test.php @@ -594,6 +594,14 @@ class RouterTest extends UnitTestCase { $expected = array('pass' => array('min-forestilling'), 'plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'year' => 2007, 'month' => 10, 'named' => array()); $this->assertEqual($result, $expected); + $this->router->reload(); + $this->router->testing = true; + $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'); + $this->assertEqual($result, $expected); + unset($this->router->testing); } function testUuidRoutes() {