Permalink
Browse files

Converted prefix to a route parameter like all other special route pa…

…rameters.
  • Loading branch information...
1 parent 532ee9c commit e8a23e09cd1f5a3a1291d98690212deeb4c95c23 @sam-at-github sam-at-github committed Feb 11, 2014
Showing 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
View
@@ -42,40 +42,33 @@
*
* 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);
@@ -447,7 +447,6 @@ public function match($url, $context = array()) {
return false;
}
- $prefixes = Router::prefixes();
$pass = array();
$query = array();
View
@@ -301,6 +301,7 @@ public static function resourceMap($resourceMap = null) {
* - `_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 @@ public static function resourceMap($resourceMap = null) {
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 @@ public static function redirect($route, $url, $options = array()) {
}
/**
- * 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 @@ public static function url($url = null, $options = array()) {
$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'],

0 comments on commit e8a23e0

Please sign in to comment.