Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Converted prefix to a route parameter like all other special route pa…
…rameters.
  • Loading branch information
sgpinkus committed Feb 11, 2014
1 parent 532ee9c commit e8a23e0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 45 deletions.
53 changes: 23 additions & 30 deletions src/Config/routes.php
Expand Up @@ -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);
1 change: 0 additions & 1 deletion src/Routing/Route/Route.php
Expand Up @@ -447,7 +447,6 @@ public function match($url, $context = array()) {
return false;
}

$prefixes = Router::prefixes();
$pass = array();
$query = array();

Expand Down
19 changes: 5 additions & 14 deletions src/Routing/Router.php
Expand Up @@ -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:
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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'],
Expand Down

0 comments on commit e8a23e0

Please sign in to comment.