Implementing Routing.prefixes.

Replaces Routing.admin and streamlines the use of Routing prefixes so they behave more consistently with admin routing.
This commit is contained in:
mark_story 2009-09-27 00:12:03 -04:00
parent 3e394f1fa8
commit 76c1c1cb8e
2 changed files with 45 additions and 44 deletions

View file

@ -38,15 +38,8 @@ class Router {
var $routes = array(); var $routes = array();
/** /**
* Caches admin setting from Configure class * List of action prefixes used in connected routes.
* * Includes admin prefix
* @var array
* @access private
*/
var $__admin = null;
/**
* List of action prefixes used in connected routes
* *
* @var array * @var array
* @access private * @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 * @return void
* @access private * @access private
* @todo Remove support for Routing.admin in future versions.
**/ **/
function __setPrefixes() { function __setPrefixes() {
$routing = Configure::read('Routing'); $routing = Configure::read('Routing');
if (isset($routing['admin'])) { if (isset($routing['admin'])) {
$this->__prefixes[] = $this->__admin = $routing['admin']; $this->__prefixes[] = $routing['admin'];
} }
if (isset($routing['prefixes'])) { if (isset($routing['prefixes'])) {
$this->__prefixes = array_merge($this->__prefixes, (array)$routing['prefixes']); $this->__prefixes = array_merge($this->__prefixes, (array)$routing['prefixes']);
@ -235,8 +230,10 @@ class Router {
if (!isset($default['action'])) { if (!isset($default['action'])) {
$default['action'] = 'index'; $default['action'] = 'index';
} }
if (isset($default[$_this->__admin])) { foreach ($_this->__prefixes as $prefix) {
$default['prefix'] = $_this->__admin; if (isset($default[$prefix])) {
$default['prefix'] = $prefix;
}
} }
if (isset($default['prefix'])) { if (isset($default['prefix'])) {
$_this->__prefixes[] = $default['prefix']; $_this->__prefixes[] = $default['prefix'];
@ -624,10 +621,6 @@ class Router {
return; return;
} }
if ($this->__admin) {
$params = array('prefix' => $this->__admin, $this->__admin => true);
}
if ($plugins = App::objects('plugin')) { if ($plugins = App::objects('plugin')) {
foreach ($plugins as $key => $value) { foreach ($plugins as $key => $value) {
$plugins[$key] = Inflector::underscore($value); $plugins[$key] = Inflector::underscore($value);
@ -636,15 +629,17 @@ class Router {
$match = array('plugin' => implode('|', $plugins)); $match = array('plugin' => implode('|', $plugins));
$this->connect('/:plugin/:controller/:action/*', array(), $match); $this->connect('/:plugin/:controller/:action/*', array(), $match);
if ($this->__admin) { foreach ($this->__prefixes as $prefix) {
$this->connect("/{$this->__admin}/:plugin/:controller", $params, $match); $params = array('prefix' => $prefix, $prefix => true);
$this->connect("/{$this->__admin}/:plugin/:controller/:action/*", $params, $match); $this->connect("/{$prefix}/:plugin/:controller", $params, $match);
$this->connect("/{$prefix}/:plugin/:controller/:action/*", $params, $match);
} }
} }
if ($this->__admin) { foreach ($this->__prefixes as $prefix) {
$this->connect("/{$this->__admin}/:controller", $params); $params = array('prefix' => $prefix, $prefix => true);
$this->connect("/{$this->__admin}/:controller/:action/*", $params); $this->connect("/{$prefix}/:controller", $params);
$this->connect("/{$prefix}/:controller/:action/*", $params);
} }
$this->connect('/:controller', array('action' => 'index')); $this->connect('/:controller', array('action' => 'index'));
$this->connect('/:controller/:action/*'); $this->connect('/:controller/:action/*');
@ -850,11 +845,12 @@ class Router {
$url['action'] = 'index'; $url['action'] = 'index';
} }
} }
if ($_this->__admin) {
if (!isset($url[$_this->__admin]) && !empty($params[$_this->__admin])) { foreach ($_this->__prefixes as $prefix) {
$url[$_this->__admin] = true; if (!isset($url[$prefix]) && !empty($params[$prefix])) {
} elseif ($_this->__admin && isset($url[$_this->__admin]) && !$url[$_this->__admin]) { $url[$prefix] = true;
unset($url[$_this->__admin]); } elseif (isset($url[$prefix]) && !$url[$prefix]) {
unset($url[$prefix]);
} }
} }
$plugin = false; $plugin = false;
@ -893,8 +889,9 @@ class Router {
} }
$named = $args = array(); $named = $args = array();
$skip = array( $skip = array_merge(
'bare', 'action', 'controller', 'plugin', 'ext', '?', '#', 'prefix', $_this->__admin array('bare', 'action', 'controller', 'plugin', 'ext', '?', '#', 'prefix'),
$_this->__prefixes
); );
$keys = array_values(array_diff(array_keys($url), $skip)); $keys = array_values(array_diff(array_keys($url), $skip));
@ -913,8 +910,10 @@ class Router {
if ($match === false) { if ($match === false) {
list($args, $named) = array(Set::filter($args, true), Set::filter($named)); list($args, $named) = array(Set::filter($args, true), Set::filter($named));
if (!empty($url[$_this->__admin])) { foreach ($_this->__prefixes as $prefix) {
$url['action'] = str_replace($_this->__admin . '_', '', $url['action']); if (!empty($url[$prefix])) {
$url['action'] = str_replace($prefix . '_', '', $url['action']);
}
} }
if (empty($named) && empty($args) && (!isset($url['action']) || $url['action'] === 'index')) { if (empty($named) && empty($args) && (!isset($url['action']) || $url['action'] === 'index')) {
@ -927,8 +926,10 @@ class Router {
array_unshift($urlOut, $url['plugin']); array_unshift($urlOut, $url['plugin']);
} }
if ($_this->__admin && isset($url[$_this->__admin])) { foreach ($_this->__prefixes as $prefix) {
array_unshift($urlOut, $_this->__admin); if (isset($url[$prefix])) {
array_unshift($urlOut, $prefix);
}
} }
$output = join('/', $urlOut) . '/'; $output = join('/', $urlOut) . '/';
} }
@ -960,8 +961,10 @@ class Router {
$output = $base . $url; $output = $base . $url;
} else { } else {
$output = $base . '/'; $output = $base . '/';
if ($_this->__admin && isset($params[$_this->__admin])) { foreach ($_this->__prefixes as $prefix) {
$output .= $_this->__admin . '/'; if (isset($params[$prefix])) {
$output .= $prefix . '/';
}
} }
if (!empty($params['plugin']) && $params['plugin'] !== $params['controller']) { if (!empty($params['plugin']) && $params['plugin'] !== $params['controller']) {
$output .= Inflector::underscore($params['plugin']) . '/'; $output .= Inflector::underscore($params['plugin']) . '/';

View file

@ -1050,16 +1050,14 @@ class RouterTest extends CakeTestCase {
*/ */
function testAdminRouting() { function testAdminRouting() {
Configure::write('Routing.admin', 'admin'); Configure::write('Routing.admin', 'admin');
Router::reload();
Router::parse('/');
Router::reload(); Router::reload();
Router::connect('/admin', array('admin' => true, 'controller' => 'users')); Router::connect('/admin', array('admin' => true, 'controller' => 'users'));
$result = Router::parse('/admin'); $result = Router::parse('/admin');
$expected = array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'users', 'action' => 'index', 'admin' => true, 'prefix' => 'admin'); $expected = array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'users', 'action' => 'index', 'admin' => true, 'prefix' => 'admin');
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = Router::url(array('admin' => true, 'controller' => 'posts', 'action' => 'index', '0', '?' => 'var=test&var2=test2')); $result = Router::url(array('admin' => true, 'controller' => 'posts', 'action' => 'index', '0', '?' => 'var=test&var2=test2'));
$expected = '/admin/posts/index/0?var=test&var2=test2'; $expected = '/admin/posts/index/0?var=test&var2=test2';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);