Merge branch '1.3-router' into 1.3-misc

This commit is contained in:
mark_story 2009-09-27 16:20:22 -04:00
commit 622e500943
4 changed files with 237 additions and 72 deletions

View file

@ -80,11 +80,27 @@
* The value of the define determines the name of the route * The value of the define determines the name of the route
* and its associated controller actions: * and its associated controller actions:
* *
* 'admin' -> admin_index() and /admin/controller/index * 'admin' -> admin_index() and /admin/controller/index
* 'superuser' -> superuser_index() and /superuser/controller/index * 'superuser' -> superuser_index() and /superuser/controller/index
*
* [Note Routing.admin is deprecated in 1.3. Use Routing.prefixes instead]
*/ */
//Configure::write('Routing.admin', 'admin'); //Configure::write('Routing.admin', 'admin');
/**
* Uncomment the define below to use CakePHP prefix routes.
*
* Set to an array of prefixes you want to use in your application. Use for
* admin or other prefixed routes.
*
* Routing.prefixes = array('admin', 'manager');
*
* Enables:
* `admin_index()` and `/admin/controller/index`
* `manager_index()` and `/manager/controller/index`
*/
//Configure::write('Routing.prefixes', array('admin'));
/** /**
* Turn off all caching application-wide. * Turn off all caching application-wide.
* *

View file

@ -80,11 +80,27 @@
* The value of the define determines the name of the route * The value of the define determines the name of the route
* and its associated controller actions: * and its associated controller actions:
* *
* 'admin' -> admin_index() and /admin/controller/index * 'admin' -> admin_index() and /admin/controller/index
* 'superuser' -> superuser_index() and /superuser/controller/index * 'superuser' -> superuser_index() and /superuser/controller/index
*
* [Note Routing.admin is deprecated in 1.3. Use Routing.prefixes instead]
*/ */
//Configure::write('Routing.admin', 'admin'); //Configure::write('Routing.admin', 'admin');
/**
* Uncomment the define below to use CakePHP prefix routes.
*
* Set to an array of prefixes you want to use in your application. Use for
* admin or other prefixed routes.
*
* Routing.prefixes = array('admin', 'manager');
*
* Enables:
* `admin_index()` and `/admin/controller/index`
* `manager_index()` and `/manager/controller/index`
*/
//Configure::write('Routing.prefixes', array('admin'));
/** /**
* Turn off all caching application-wide. * Turn off all caching application-wide.
* *

View file

@ -1,6 +1,4 @@
<?php <?php
/* SVN FILE: $Id$ */
/** /**
* Parses the request URL into controller, action, and parameters. * Parses the request URL into controller, action, and parameters.
* *
@ -20,27 +18,16 @@
* @package cake * @package cake
* @subpackage cake.cake.libs * @subpackage cake.cake.libs
* @since CakePHP(tm) v 0.2.9 * @since CakePHP(tm) v 0.2.9
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License * @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/ */
/**
* Included libraries.
*
*/
if (!class_exists('Object')) {
require LIBS . 'object.php';
}
/** /**
* Parses the request URL into controller, action, and parameters. * Parses the request URL into controller, action, and parameters.
* *
* @package cake * @package cake
* @subpackage cake.cake.libs * @subpackage cake.cake.libs
*/ */
class Router extends Object { class Router {
/** /**
* Array of routes * Array of routes
@ -51,15 +38,8 @@ class Router extends Object {
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
@ -177,6 +157,33 @@ class Router extends Object {
*/ */
var $__defaultsMapped = false; var $__defaultsMapped = false;
/**
* Constructor for Router.
* Builds __prefixes
*
* @return void
**/
function Router() {
$this->__setPrefixes();
}
/**
* 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[] = $routing['admin'];
}
if (isset($routing['prefixes'])) {
$this->__prefixes = array_merge($this->__prefixes, (array)$routing['prefixes']);
}
}
/** /**
* Gets a reference to the Router object instance * Gets a reference to the Router object instance
* *
@ -189,7 +196,6 @@ class Router extends Object {
if (!$instance) { if (!$instance) {
$instance[0] =& new Router(); $instance[0] =& new Router();
$instance[0]->__admin = Configure::read('Routing.admin');
} }
return $instance[0]; return $instance[0];
} }
@ -210,11 +216,11 @@ class Router extends Object {
/** /**
* Returns this object's routes array. Returns false if there are no routes available. * 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 string $route An empty string, or a route string "/"
* @param array $default NULL or an array describing the default route * @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 array $params An array matching the named elements in the route to regular expressions which that element should match.
* @see routes * @see routes
* @return array Array of routes * @return array Array of routes
* @access public * @access public
* @static * @static
*/ */
@ -224,8 +230,10 @@ class Router extends Object {
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'];
@ -613,10 +621,6 @@ class Router extends Object {
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);
@ -625,15 +629,17 @@ class Router extends Object {
$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/*');
@ -735,7 +741,7 @@ class Router extends Object {
foreach (get_class_vars('Router') as $key => $val) { foreach (get_class_vars('Router') as $key => $val) {
$_this->{$key} = $val; $_this->{$key} = $val;
} }
$_this->__admin = Configure::read('Routing.admin'); $_this->__setPrefixes();
} }
/** /**
@ -839,11 +845,12 @@ class Router extends Object {
$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;
@ -882,8 +889,9 @@ class Router extends Object {
} }
$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));
@ -902,8 +910,10 @@ class Router extends Object {
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')) {
@ -915,9 +925,11 @@ class Router extends Object {
if (isset($url['plugin']) && $url['plugin'] != $url['controller']) { if (isset($url['plugin']) && $url['plugin'] != $url['controller']) {
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) . '/';
} }
@ -949,8 +961,10 @@ class Router extends Object {
$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

@ -1,6 +1,4 @@
<?php <?php
/* SVN FILE: $Id$ */
/** /**
* RouterTest file * RouterTest file
* *
@ -20,9 +18,6 @@
* @package cake * @package cake
* @subpackage cake.tests.cases.libs * @subpackage cake.tests.cases.libs
* @since CakePHP(tm) v 1.2.0.4206 * @since CakePHP(tm) v 1.2.0.4206
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/ */
App::import('Core', array('Router', 'Debugger')); App::import('Core', array('Router', 'Debugger'));
@ -46,11 +41,21 @@ class RouterTest extends CakeTestCase {
* @return void * @return void
*/ */
function setUp() { function setUp() {
Configure::write('Routing.admin', null); $this->_routing = Configure::read('Routing');
Configure::write('Routing', array('admin' => null, 'prefixes' => array()));
Router::reload(); Router::reload();
$this->router =& Router::getInstance(); $this->router =& Router::getInstance();
} }
/**
* end the test and reset the environment
*
* @return void
**/
function endTest() {
Configure::write('Routing', $this->_routing);
}
/** /**
* testReturnedInstanceReference method * testReturnedInstanceReference method
* *
@ -1023,23 +1028,47 @@ class RouterTest extends CakeTestCase {
} }
/** /**
* testAdminRouting method * Test that Routing.prefixes and Routing.admin are used when a Router instance is created
* or reset
*
* @return void
**/
function testRoutingPrefixesSetting() {
$restore = Configure::read('Routing');
Configure::write('Routing.admin', 'admin');
Configure::write('Routing.prefixes', array('member', 'super_user'));
Router::reload();
$result = Router::prefixes();
$expected = array('admin', 'member', 'super_user');
$this->assertEqual($result, $expected);
Configure::write('Routing.prefixes', 'member');
Router::reload();
$result = Router::prefixes();
$expected = array('admin', 'member');
$this->assertEqual($result, $expected);
Configure::write('Routing', $restore);
}
/**
* test compatibility with old Routing.admin config setting.
* *
* @access public * @access public
* @return void * @return void
* @todo Once Routing.admin is removed update these tests.
*/ */
function testAdminRouting() { function testAdminRoutingCompatibility() {
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);
@ -1086,8 +1115,15 @@ class RouterTest extends CakeTestCase {
$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => 'var=test&var2=test2')); $result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => 'var=test&var2=test2'));
$expected = '/beheer/posts/index/0?var=test&var2=test2'; $expected = '/beheer/posts/index/0?var=test&var2=test2';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
}
Configure::write('Routing.admin', 'admin'); /**
* Test prefix routing and plugin combinations
*
* @return void
**/
function testPrefixRoutingAndPlugins() {
Configure::write('Routing.prefixes', array('admin'));
$paths = App::path('plugins'); $paths = App::path('plugins');
App::build(array( App::build(array(
'plugins' => array( 'plugins' => array(
@ -1358,12 +1394,14 @@ class RouterTest extends CakeTestCase {
} }
/** /**
* testUrlGenerationWithPrefixes method * test url generation with legacy (1.2) style prefix routes.
* *
* @access public * @access public
* @return void * @return void
* @todo Remove tests related to legacy style routes.
* @see testUrlGenerationWithAutoPrefixes
*/ */
function testUrlGenerationWithPrefixes() { function testUrlGenerationWithLegacyPrefixes() {
Router::reload(); Router::reload();
Router::connect('/protected/:controller/:action/*', array( Router::connect('/protected/:controller/:action/*', array(
'controller' => 'users', 'controller' => 'users',
@ -1420,6 +1458,87 @@ class RouterTest extends CakeTestCase {
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
} }
/**
* test newer style automatically generated prefix routes.
*
* @return void
**/
function testUrlGenerationWithAutoPrefixes() {
Configure::write('Routing.prefixes', array('protected'));
Router::reload();
Router::parse('/');
Router::setRequestInfo(array(
array('plugin' => null, 'controller' => 'images', 'action' => 'index', 'pass' => array(), 'prefix' => null, 'protected' => false, 'form' => array(), 'url' => array('url' => 'images/index')),
array('base' => '', 'here' => '/images/index', 'webroot' => '/')
));
$result = Router::url(array('controller' => 'images', 'action' => 'add'));
$expected = '/images/add';
$this->assertEqual($result, $expected);
$result = Router::url(array('controller' => 'images', 'action' => 'add', 'protected' => true));
$expected = '/protected/images/add';
$this->assertEqual($result, $expected);
$result = Router::url(array('action' => 'edit', 1));
$expected = '/images/edit/1';
$this->assertEqual($result, $expected);
$result = Router::url(array('action' => 'edit', 1, 'protected' => true));
$expected = '/protected/images/edit/1';
$this->assertEqual($result, $expected);
$result = Router::url(array('action' => 'protected_edit', 1, 'protected' => true));
$expected = '/protected/images/edit/1';
$this->assertEqual($result, $expected);
$result = Router::url(array('action' => 'edit', 1, 'protected' => true));
$expected = '/protected/images/edit/1';
$this->assertEqual($result, $expected);
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1));
$expected = '/others/edit/1';
$this->assertEqual($result, $expected);
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true));
$expected = '/protected/others/edit/1';
$this->assertEqual($result, $expected);
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, 'page' => 1));
$expected = '/protected/others/edit/1/page:1';
$this->assertEqual($result, $expected);
Router::connectNamed(array('random'));
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, 'random' => 'my-value'));
$expected = '/protected/others/edit/1/random:my-value';
$this->assertEqual($result, $expected);
}
/**
* test that auto-generated prefix routes persist
*
* @return void
**/
function testAutoPrefixRoutePersistence() {
Configure::write('Routing.prefixes', array('protected'));
Router::reload();
Router::parse('/');
Router::setRequestInfo(array(
array('plugin' => null, 'controller' => 'images', 'action' => 'index', 'pass' => array(), 'prefix' => 'protected', 'protected' => true, 'form' => array(), 'url' => array('url' => 'protected/images/index')),
array('base' => '', 'here' => '/protected/images/index', 'webroot' => '/')
));
$result = Router::url(array('controller' => 'images', 'action' => 'add'));
$expected = '/protected/images/add';
$this->assertEqual($result, $expected);
$result = Router::url(array('controller' => 'images', 'action' => 'add', 'protected' => false));
$expected = '/images/add';
$this->assertEqual($result, $expected);
}
/** /**
* testRemoveBase method * testRemoveBase method
* *
@ -1660,7 +1779,7 @@ class RouterTest extends CakeTestCase {
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = Router::prefixes(); $result = Router::prefixes();
$expected = array('protected', 'admin'); $expected = array('admin', 'protected');
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
} }