Browse Source

Converted prefix to a route parameter like all other special route parameters.

Sam 12 years ago
parent
commit
e8a23e09cd
3 changed files with 28 additions and 45 deletions
  1. 23 30
      src/Config/routes.php
  2. 0 1
      src/Routing/Route/Route.php
  3. 5 14
      src/Routing/Router.php

+ 23 - 30
src/Config/routes.php

@@ -42,40 +42,33 @@ use Cake\Utility\Inflector;
  *
  * You can disable the connection of default routes by deleting the require inside APP/Config/routes.php.
  */
-$prefixes = Router::prefixes();
-
-if ($plugins = Plugin::loaded()) {
-	foreach ($plugins as $key => $value) {
-		$plugins[$key] = Inflector::underscore($value);
-	}
-	$pluginPattern = implode('|', $plugins);
-	$match = ['plugin' => $pluginPattern];
-	$shortParams = [
-		'routeClass' => 'Cake\Routing\Route\PluginShortRoute',
-		'plugin' => $pluginPattern,
-		'_name' => '_plugin._controller:index',
-	];
 
-	foreach ($prefixes as $prefix) {
-		$params = ['prefix' => $prefix];
-		$indexParams = $params + ['action' => 'index'];
-		Router::connect("/{$prefix}/:plugin", $indexParams, $shortParams);
-		Router::connect("/{$prefix}/:plugin/:controller", $indexParams, $match);
-		Router::connect("/{$prefix}/:plugin/:controller/:action/*", $params, $match);
-	}
-	Router::connect('/:plugin', ['action' => 'index'], $shortParams);
-	Router::connect('/:plugin/:controller', ['action' => 'index'], $match);
-	Router::connect('/:plugin/:controller/:action/*', [], $match);
+$prefixes = Router::prefixes();
+$prefixPattern = implode('|', $prefixes);
+$plugins = Plugin::loaded();
+foreach ($plugins as $key => $value) {
+	$plugins[$key] = Inflector::underscore($value);
 }
+$pluginPattern = implode('|', $plugins);
+$indexParams = ['action' => 'index'];
+$match = ['prefix' => $prefixPattern, 'plugin' => $pluginPattern];
 
-foreach ($prefixes as $prefix) {
-	$params = ['prefix' => $prefix];
-	$indexParams = $params + ['action' => 'index'];
-	Router::connect("/{$prefix}/:controller", $indexParams);
-	Router::connect("/{$prefix}/:controller/:action/*", $params);
+if($prefixPattern && $pluginPattern) {
+	Router::connect("/:prefix/:plugin", $indexParams, $match + ['routeClass' => 'Cake\Routing\Route\PluginShortRoute']);
+	Router::connect("/:prefix/:plugin/:controller", $indexParams, $match);
+	Router::connect("/:prefix/:plugin/:controller/:action/*", [], $match);
+}
+else if($pluginPattern) {
+	Router::connect("/:plugin", $indexParams, $match + ['routeClass' => 'Cake\Routing\Route\PluginShortRoute']);
+	Router::connect("/:plugin/:controller", $indexParams, $match);
+	Router::connect("/:plugin/:controller/:action/*", [], $match);
+}
+else if($prefixPattern) {
+	Router::connect("/:prefix", $indexParams, $match );
+	Router::connect("/:prefix/:controller", $indexParams, $match);
+	Router::connect("/:prefix/:controller/:action/*", [], $match);
 }
 Router::connect('/:controller', ['action' => 'index']);
 Router::connect('/:controller/:action/*');
 
-unset($params, $indexParams, $prefix, $prefixes, $shortParams, $match,
-	$pluginPattern, $plugins, $key, $value);
+unset($prefixes, $prefixPattern, $plugins, $pluginPattern, $indexParams, $match);

+ 0 - 1
src/Routing/Route/Route.php

@@ -447,7 +447,6 @@ class Route {
 			return false;
 		}
 
-		$prefixes = Router::prefixes();
 		$pass = array();
 		$query = array();
 

+ 5 - 14
src/Routing/Router.php

@@ -301,6 +301,7 @@ class Router {
  * - `_name` Used to define a specific name for routes. This can be used to optimize
  *   reverse routing lookups. If undefined a name will be generated for each
  *   connected route.
+ * - `_ext` todo explain.
  *
  * You can also add additional conditions for matching routes to the $defaults array.
  * The following conditions can be used:
@@ -329,14 +330,6 @@ class Router {
 	public static function connect($route, $defaults = array(), $options = array()) {
 		static::$initialized = true;
 
-		if (!empty($defaults['prefix'])) {
-			static::$_prefixes[] = $defaults['prefix'];
-			static::$_prefixes = array_keys(array_flip(static::$_prefixes));
-		}
-		if (empty($defaults['prefix'])) {
-			unset($defaults['prefix']);
-		}
-
 		$defaults += array('plugin' => null);
 		if (empty($options['action'])) {
 			$defaults += array('action' => 'index');
@@ -397,7 +390,8 @@ class Router {
 	}
 
 /**
- * Creates REST resource routes for the given controller(s).
+ * Generate REST resource routes for the given controller(s). A quick way to generate a default routes to a set
+ *  of REST resources (controller(s)).
  *
  * ### Usage
  *
@@ -795,13 +789,10 @@ class Router {
 				$url['action'] = $params['action'];
 			}
 
-			// Keep the current prefix around, or remove it if its falsey
-			if (!empty($params['prefix']) && !isset($url['prefix'])) {
+			// Keep the current prefix around if none set.
+			if (isset($params['prefix']) && !isset($url['prefix'])) {
 				$url['prefix'] = $params['prefix'];
 			}
-			if (empty($url['prefix'])) {
-				unset($url['prefix']);
-			}
 
 			$url += array(
 				'controller' => $params['controller'],