Skip to content

Commit

Permalink
Fix incorrect handling of inflections in InflectedRoute
Browse files Browse the repository at this point in the history
When inflecting plugin/controller names in URL params we must also
ensure that default values have been inflected in the same way or match
failures will occur.

Refs #4120
  • Loading branch information
markstory committed Jul 31, 2014
1 parent 8b6f04b commit 96cb76b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/Routing/Route/InflectedRoute.php
Expand Up @@ -23,6 +23,16 @@
*/
class InflectedRoute extends Route {

/**
* Flag for tracking whether or not the defaults have been inflected.
*
* Default values need to be inflected so that they match the inflections that match()
* will create.
*
* @var bool
*/
protected $_inflectedDefaults = false;

/**
* Parses a string URL into an array. If it mathes, it will convert the prefix, controller and
* plugin keys to their camelized form
Expand Down Expand Up @@ -55,13 +65,28 @@ public function parse($url) {
* @return mixed either false or a string URL.
*/
public function match(array $url, array $context = array()) {
$url = $this->_underscore($url);
if (!$this->_inflectedDefaults) {
$this->_inflectedDefaults = true;
$this->defaults = $this->_underscore($this->defaults);
}
return parent::match($url, $context);
}

/**
* Helper method for underscoring keys in a URL array.
*
* @param array $url An array of URL keys.
* @return array
*/
protected function _underscore($url) {
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);
return $url;
}

}
16 changes: 16 additions & 0 deletions tests/TestCase/Routing/RouterTest.php
Expand Up @@ -981,6 +981,22 @@ public function testUrlGenerationWithPrefix() {
$this->assertEquals($expected, $result);
}

/**
* Test URL generation inside a prefixed plugin.
*
* @return void
*/
public function testUrlGenerationPrefixedPlugin() {
Router::prefix('admin', function($routes) {
$routes->plugin('MyPlugin', function($routes) {
$routes->fallbacks();
});
});
$result = Router::url(['prefix' => 'admin', 'plugin' => 'MyPlugin', 'controller' => 'Forms', 'action' => 'edit', 2]);
$expected = '/admin/my_plugin/forms/edit/2';
$this->assertEquals($expected, $result);
}

/**
* testUrlGenerationWithExtensions method
*
Expand Down

0 comments on commit 96cb76b

Please sign in to comment.