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

@ -82,9 +82,25 @@
*
* 'admin' -> admin_index() and /admin/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');
/**
* 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.
*

View file

@ -82,9 +82,25 @@
*
* 'admin' -> admin_index() and /admin/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');
/**
* 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.
*

View file

@ -1,6 +1,4 @@
<?php
/* SVN FILE: $Id$ */
/**
* Parses the request URL into controller, action, and parameters.
*
@ -20,27 +18,16 @@
* @package cake
* @subpackage cake.cake.libs
* @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
*/
/**
* Included libraries.
*
*/
if (!class_exists('Object')) {
require LIBS . 'object.php';
}
/**
* Parses the request URL into controller, action, and parameters.
*
* @package cake
* @subpackage cake.cake.libs
*/
class Router extends Object {
class Router {
/**
* Array of routes
@ -51,15 +38,8 @@ class Router extends Object {
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
@ -177,6 +157,33 @@ class Router extends Object {
*/
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
*
@ -189,7 +196,6 @@ class Router extends Object {
if (!$instance) {
$instance[0] =& new Router();
$instance[0]->__admin = Configure::read('Routing.admin');
}
return $instance[0];
}
@ -224,8 +230,10 @@ class Router extends Object {
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'];
@ -613,10 +621,6 @@ class Router extends Object {
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);
@ -625,15 +629,17 @@ class Router extends Object {
$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/*');
@ -735,7 +741,7 @@ class Router extends Object {
foreach (get_class_vars('Router') as $key => $val) {
$_this->{$key} = $val;
}
$_this->__admin = Configure::read('Routing.admin');
$_this->__setPrefixes();
}
/**
@ -839,11 +845,12 @@ class Router extends Object {
$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;
@ -882,8 +889,9 @@ class Router extends Object {
}
$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));
@ -902,8 +910,10 @@ class Router extends Object {
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')) {
@ -916,8 +926,10 @@ class Router extends Object {
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) . '/';
}
@ -949,8 +961,10 @@ class Router extends Object {
$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']) . '/';

View file

@ -1,6 +1,4 @@
<?php
/* SVN FILE: $Id$ */
/**
* RouterTest file
*
@ -20,9 +18,6 @@
* @package cake
* @subpackage cake.tests.cases.libs
* @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
*/
App::import('Core', array('Router', 'Debugger'));
@ -46,11 +41,21 @@ class RouterTest extends CakeTestCase {
* @return void
*/
function setUp() {
Configure::write('Routing.admin', null);
$this->_routing = Configure::read('Routing');
Configure::write('Routing', array('admin' => null, 'prefixes' => array()));
Router::reload();
$this->router =& Router::getInstance();
}
/**
* end the test and reset the environment
*
* @return void
**/
function endTest() {
Configure::write('Routing', $this->_routing);
}
/**
* 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
* @return void
* @todo Once Routing.admin is removed update these tests.
*/
function testAdminRouting() {
function testAdminRoutingCompatibility() {
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);
@ -1086,8 +1115,15 @@ class RouterTest extends CakeTestCase {
$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => 'var=test&var2=test2'));
$expected = '/beheer/posts/index/0?var=test&var2=test2';
$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');
App::build(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
* @return void
* @todo Remove tests related to legacy style routes.
* @see testUrlGenerationWithAutoPrefixes
*/
function testUrlGenerationWithPrefixes() {
function testUrlGenerationWithLegacyPrefixes() {
Router::reload();
Router::connect('/protected/:controller/:action/*', array(
'controller' => 'users',
@ -1420,6 +1458,87 @@ class RouterTest extends CakeTestCase {
$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
*
@ -1660,7 +1779,7 @@ class RouterTest extends CakeTestCase {
$this->assertEqual($result, $expected);
$result = Router::prefixes();
$expected = array('protected', 'admin');
$expected = array('admin', 'protected');
$this->assertEqual($result, $expected);
}