From 76c1c1cb8e4325956a99c5592c3ac62dec5ca051 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 27 Sep 2009 00:12:03 -0400 Subject: [PATCH] Implementing Routing.prefixes. Replaces Routing.admin and streamlines the use of Routing prefixes so they behave more consistently with admin routing. --- cake/libs/router.php | 85 ++++++++++++++------------- cake/tests/cases/libs/router.test.php | 4 +- 2 files changed, 45 insertions(+), 44 deletions(-) diff --git a/cake/libs/router.php b/cake/libs/router.php index 985cc5879..4eca58737 100644 --- a/cake/libs/router.php +++ b/cake/libs/router.php @@ -38,15 +38,8 @@ class Router { var $routes = array(); /** - * Caches admin setting from Configure class - * - * @var array - * @access private - */ - var $__admin = null; - -/** - * List of action prefixes used in connected routes + * List of action prefixes used in connected routes. + * Includes admin prefix * * @var array * @access private @@ -175,15 +168,17 @@ class Router { } /** - * Sets the Routing prefixes. + * Sets the Routing prefixes. Includes compatibilty for existing Routing.admin + * configurations. * * @return void * @access private + * @todo Remove support for Routing.admin in future versions. **/ function __setPrefixes() { $routing = Configure::read('Routing'); if (isset($routing['admin'])) { - $this->__prefixes[] = $this->__admin = $routing['admin']; + $this->__prefixes[] = $routing['admin']; } if (isset($routing['prefixes'])) { $this->__prefixes = array_merge($this->__prefixes, (array)$routing['prefixes']); @@ -221,11 +216,11 @@ class Router { /** * Returns this object's routes array. Returns false if there are no routes available. * - * @param string $route An empty string, or a route string "/" - * @param array $default NULL or an array describing the default route - * @param array $params An array matching the named elements in the route to regular expressions which that element should match. + * @param string $route An empty string, or a route string "/" + * @param array $default NULL or an array describing the default route + * @param array $params An array matching the named elements in the route to regular expressions which that element should match. * @see routes - * @return array Array of routes + * @return array Array of routes * @access public * @static */ @@ -235,8 +230,10 @@ class Router { if (!isset($default['action'])) { $default['action'] = 'index'; } - if (isset($default[$_this->__admin])) { - $default['prefix'] = $_this->__admin; + foreach ($_this->__prefixes as $prefix) { + if (isset($default[$prefix])) { + $default['prefix'] = $prefix; + } } if (isset($default['prefix'])) { $_this->__prefixes[] = $default['prefix']; @@ -624,10 +621,6 @@ class Router { return; } - if ($this->__admin) { - $params = array('prefix' => $this->__admin, $this->__admin => true); - } - if ($plugins = App::objects('plugin')) { foreach ($plugins as $key => $value) { $plugins[$key] = Inflector::underscore($value); @@ -636,15 +629,17 @@ class Router { $match = array('plugin' => implode('|', $plugins)); $this->connect('/:plugin/:controller/:action/*', array(), $match); - if ($this->__admin) { - $this->connect("/{$this->__admin}/:plugin/:controller", $params, $match); - $this->connect("/{$this->__admin}/:plugin/:controller/:action/*", $params, $match); + foreach ($this->__prefixes as $prefix) { + $params = array('prefix' => $prefix, $prefix => true); + $this->connect("/{$prefix}/:plugin/:controller", $params, $match); + $this->connect("/{$prefix}/:plugin/:controller/:action/*", $params, $match); } } - if ($this->__admin) { - $this->connect("/{$this->__admin}/:controller", $params); - $this->connect("/{$this->__admin}/:controller/:action/*", $params); + foreach ($this->__prefixes as $prefix) { + $params = array('prefix' => $prefix, $prefix => true); + $this->connect("/{$prefix}/:controller", $params); + $this->connect("/{$prefix}/:controller/:action/*", $params); } $this->connect('/:controller', array('action' => 'index')); $this->connect('/:controller/:action/*'); @@ -850,11 +845,12 @@ class Router { $url['action'] = 'index'; } } - if ($_this->__admin) { - if (!isset($url[$_this->__admin]) && !empty($params[$_this->__admin])) { - $url[$_this->__admin] = true; - } elseif ($_this->__admin && isset($url[$_this->__admin]) && !$url[$_this->__admin]) { - unset($url[$_this->__admin]); + + foreach ($_this->__prefixes as $prefix) { + if (!isset($url[$prefix]) && !empty($params[$prefix])) { + $url[$prefix] = true; + } elseif (isset($url[$prefix]) && !$url[$prefix]) { + unset($url[$prefix]); } } $plugin = false; @@ -893,8 +889,9 @@ class Router { } $named = $args = array(); - $skip = array( - 'bare', 'action', 'controller', 'plugin', 'ext', '?', '#', 'prefix', $_this->__admin + $skip = array_merge( + array('bare', 'action', 'controller', 'plugin', 'ext', '?', '#', 'prefix'), + $_this->__prefixes ); $keys = array_values(array_diff(array_keys($url), $skip)); @@ -913,8 +910,10 @@ class Router { if ($match === false) { list($args, $named) = array(Set::filter($args, true), Set::filter($named)); - if (!empty($url[$_this->__admin])) { - $url['action'] = str_replace($_this->__admin . '_', '', $url['action']); + foreach ($_this->__prefixes as $prefix) { + if (!empty($url[$prefix])) { + $url['action'] = str_replace($prefix . '_', '', $url['action']); + } } if (empty($named) && empty($args) && (!isset($url['action']) || $url['action'] === 'index')) { @@ -926,9 +925,11 @@ class Router { if (isset($url['plugin']) && $url['plugin'] != $url['controller']) { array_unshift($urlOut, $url['plugin']); } - - if ($_this->__admin && isset($url[$_this->__admin])) { - array_unshift($urlOut, $_this->__admin); + + foreach ($_this->__prefixes as $prefix) { + if (isset($url[$prefix])) { + array_unshift($urlOut, $prefix); + } } $output = join('/', $urlOut) . '/'; } @@ -960,8 +961,10 @@ class Router { $output = $base . $url; } else { $output = $base . '/'; - if ($_this->__admin && isset($params[$_this->__admin])) { - $output .= $_this->__admin . '/'; + foreach ($_this->__prefixes as $prefix) { + if (isset($params[$prefix])) { + $output .= $prefix . '/'; + } } if (!empty($params['plugin']) && $params['plugin'] !== $params['controller']) { $output .= Inflector::underscore($params['plugin']) . '/'; diff --git a/cake/tests/cases/libs/router.test.php b/cake/tests/cases/libs/router.test.php index d183aed71..e7818d306 100644 --- a/cake/tests/cases/libs/router.test.php +++ b/cake/tests/cases/libs/router.test.php @@ -1050,16 +1050,14 @@ class RouterTest extends CakeTestCase { */ function testAdminRouting() { Configure::write('Routing.admin', 'admin'); - Router::reload(); - Router::parse('/'); Router::reload(); Router::connect('/admin', array('admin' => true, 'controller' => 'users')); $result = Router::parse('/admin'); + $expected = array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'users', 'action' => 'index', 'admin' => true, 'prefix' => 'admin'); $this->assertEqual($result, $expected); - $result = Router::url(array('admin' => true, 'controller' => 'posts', 'action' => 'index', '0', '?' => 'var=test&var2=test2')); $expected = '/admin/posts/index/0?var=test&var2=test2'; $this->assertEqual($result, $expected);