Permalink
Browse files

Implementing Routing.prefixes.

Replaces Routing.admin and streamlines the use of Routing prefixes so they behave more consistently with admin routing.
  • Loading branch information...
1 parent 3e394f1 commit 76c1c1cb8e4325956a99c5592c3ac62dec5ca051 @markstory markstory committed Sep 27, 2009
Showing with 45 additions and 44 deletions.
  1. +44 −41 cake/libs/router.php
  2. +1 −3 cake/tests/cases/libs/router.test.php
View
@@ -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 @@ function 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 @@ function getNamedExpressions() {
/**
* 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 @@ function connect($route, $default = array(), $params = array()) {
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 @@ function __connectDefaultRoutes() {
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 @@ function __connectDefaultRoutes() {
$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 @@ function url($url = null, $full = false) {
$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;
@ADmad

ADmad Jun 13, 2017

Member

@sebastienbarre Please open an issue instead of commenting on a commit, especially a very old one.

+ } elseif (isset($url[$prefix]) && !$url[$prefix]) {
+ unset($url[$prefix]);
}
}
$plugin = false;
@@ -893,8 +889,9 @@ function url($url = null, $full = false) {
}
$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 @@ function url($url = null, $full = false) {
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 @@ function url($url = null, $full = false) {
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 @@ function url($url = null, $full = false) {
$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']) . '/';
@@ -1050,16 +1050,14 @@ function testRoutingPrefixesSetting() {
*/
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);

0 comments on commit 76c1c1c

Please sign in to comment.