Skip to content

Commit

Permalink
Adding and using a InflectedRoute class for default routes.
Browse files Browse the repository at this point in the history
This will maintain backwards compatibility while still leaving room for
extensibility
  • Loading branch information
lorenzo committed Jun 9, 2014
1 parent 5a0a796 commit 52d98f0
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 7 deletions.
29 changes: 24 additions & 5 deletions src/Config/routes.php
Expand Up @@ -58,23 +58,42 @@
];

if ($prefixPattern && $pluginPattern) {
$match = ['prefix' => $prefixPattern, 'plugin' => $pluginPattern, 'defaultRoute' => true];
$match = [
'prefix' => $prefixPattern,
'plugin' => $pluginPattern,
'defaultRoute' => true,
'routeClass' => 'Cake\Routing\Route\InflectedRoute'
];
Router::connect('/:prefix/:plugin', $indexParams, $match + $pluginShortMatch);
Router::connect('/:prefix/:plugin/:controller', $indexParams, $match);
Router::connect('/:prefix/:plugin/:controller/:action/*', [], $match);
}
if ($pluginPattern) {
$match = ['plugin' => $pluginPattern, 'defaultRoute' => true];
$match = [
'plugin' => $pluginPattern,
'defaultRoute' => true,
'routeClass' => 'Cake\Routing\Route\InflectedRoute'
];
Router::connect('/:plugin', $indexParams, $match + $pluginShortMatch);
Router::connect('/:plugin/:controller', $indexParams, $match);
Router::connect('/:plugin/:controller/:action/*', [], $match);
}
if ($prefixPattern) {
$match = ['prefix' => $prefixPattern, 'defaultRoute' => true];
$match = [
'prefix' => $prefixPattern,
'defaultRoute' => true,
'routeClass' => 'Cake\Routing\Route\InflectedRoute'
];
Router::connect('/:prefix/:controller', $indexParams, $match);
Router::connect('/:prefix/:controller/:action/*', [], $match);
}
Router::connect('/:controller', ['action' => 'index']);
Router::connect('/:controller/:action/*');
Router::connect('/:controller', ['action' => 'index'], [
'defaultRoute' => true,
'routeClass' => 'Cake\Routing\Route\InflectedRoute'
]);
Router::connect('/:controller/:action/*', [], [
'defaultRoute' => true,
'routeClass' => 'Cake\Routing\Route\InflectedRoute'
]);

unset($prefixes, $prefixPattern, $plugins, $pluginPattern, $indexParams, $match);
68 changes: 68 additions & 0 deletions src/Routing/Route/InflectedRoute.php
@@ -0,0 +1,68 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Routing\Route;

use Cake\Routing\Route\Route;
use Cake\Utility\Inflector;

/**
* This route class will transparently inflect the controller and plugin routing
* parameters, so that requesting `/my_controller` is parsed as `['controller' => 'MyController']`
*/
class InflectedRoute extends Route {

/**
* Parses a string URL into an array. If it mathes, it will convert the controller and
* plugin keys to their camelized form
*
* @param string $url The URL to parse
* @return mixed false on failure, or an array of request parameters
*/
public function parse($url) {
$params = parent::parse($url);
if (!$params) {
return false;
}
if (!empty($params['controller'])) {
$params['controller'] = Inflector::camelize($params['controller']);
}
if (!empty($params['plugin'])) {
$params['plugin'] = Inflector::camelize($params['plugin']);
}
return $params;
}

/**
* Reverse route plugin shortcut URLs. If the plugin and controller
* are not the same the match is an auto fail.
*
* @param array $url Array of parameters to convert to a string.
* @param array $context An array of the current request context.
* Contains information such as the current host, scheme, port, and base
* directory.
* @return mixed either false or a string URL.
*/
public function match(array $url, array $context = array()) {
if (!empty($url['controller'])) {
$url['controller'] = Inflector::underscore($url['controller']);
}
if (!empty($url['plugin'])) {
$url['plugin'] = Inflector::underscore($url['plugin']);
}
return parent::match($url, $context);
}

}

4 changes: 2 additions & 2 deletions src/Routing/Route/PluginShortRoute.php
Expand Up @@ -14,14 +14,14 @@
*/
namespace Cake\Routing\Route;

use Cake\Routing\Route\Route;
use Cake\Routing\Route\InflectedRoute;

/**
* Plugin short route, that copies the plugin param to the controller parameters
* It is used for supporting /:plugin routes.
*
*/
class PluginShortRoute extends Route {
class PluginShortRoute extends InflectedRoute {

/**
* Parses a string URL into an array. If a plugin key is found, it will be copied to the
Expand Down

0 comments on commit 52d98f0

Please sign in to comment.