From 52d98f005f7f5a1c670d8bdea9062a68b2933589 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Mon, 9 Jun 2014 20:03:20 +0200 Subject: [PATCH] Adding and using a InflectedRoute class for default routes. This will maintain backwards compatibility while still leaving room for extensibility --- src/Config/routes.php | 29 +++++++++-- src/Routing/Route/InflectedRoute.php | 68 ++++++++++++++++++++++++++ src/Routing/Route/PluginShortRoute.php | 4 +- 3 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 src/Routing/Route/InflectedRoute.php diff --git a/src/Config/routes.php b/src/Config/routes.php index 4c080a26bdf..065a9968909 100644 --- a/src/Config/routes.php +++ b/src/Config/routes.php @@ -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); diff --git a/src/Routing/Route/InflectedRoute.php b/src/Routing/Route/InflectedRoute.php new file mode 100644 index 00000000000..805904cc449 --- /dev/null +++ b/src/Routing/Route/InflectedRoute.php @@ -0,0 +1,68 @@ + '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); + } + +} + diff --git a/src/Routing/Route/PluginShortRoute.php b/src/Routing/Route/PluginShortRoute.php index ead977ad865..13e630bb235 100644 --- a/src/Routing/Route/PluginShortRoute.php +++ b/src/Routing/Route/PluginShortRoute.php @@ -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