Permalink
Browse files

Fixing issues where route parameters that overlapped could cause rout…

…e compilation errors. Tests added. Fixes #565
  • Loading branch information...
1 parent 1c5898d commit fbaabad6bb5d6135a050c046f66e24263cb71e22 @markstory markstory committed Apr 10, 2010
Showing with 22 additions and 8 deletions.
  1. +7 −8 cake/libs/router.php
  2. +15 −0 cake/tests/cases/libs/router.test.php
View
@@ -1348,35 +1348,34 @@ function _writeRoute() {
return;
}
$route = $this->template;
- $names = $replacements = $search = array();
+ $names = $routeParams = array();
$parsed = preg_quote($this->template, '#');
preg_match_all('#:([A-Za-z0-9_-]+[A-Z0-9a-z])#', $route, $namedElements);
foreach ($namedElements[1] as $i => $name) {
+ $search = '\\' . $namedElements[0][$i];
if (isset($this->options[$name])) {
$option = null;
if ($name !== 'plugin' && array_key_exists($name, $this->defaults)) {
$option = '?';
}
$slashParam = '/\\' . $namedElements[0][$i];
if (strpos($parsed, $slashParam) !== false) {
- $replacements[] = '(?:/(?P<' . $name . '>' . $this->options[$name] . ')' . $option . ')' . $option;
- $search[] = $slashParam;
+ $routeParams[$slashParam] = '(?:/(?P<' . $name . '>' . $this->options[$name] . ')' . $option . ')' . $option;
} else {
- $search[] = '\\' . $namedElements[0][$i];
- $replacements[] = '(?:(?P<' . $name . '>' . $this->options[$name] . ')' . $option . ')' . $option;
+ $routeParams[$search] = '(?:(?P<' . $name . '>' . $this->options[$name] . ')' . $option . ')' . $option;
}
} else {
- $replacements[] = '(?:(?P<' . $name . '>[^/]+))';
- $search[] = '\\' . $namedElements[0][$i];
+ $routeParams[$search] = '(?:(?P<' . $name . '>[^/]+))';
}
$names[] = $name;
}
if (preg_match('#\/\*$#', $route, $m)) {
$parsed = preg_replace('#/\\\\\*$#', '(?:/(?P<_args_>.*))?', $parsed);
$this->_greedy = true;
}
- $parsed = str_replace($search, $replacements, $parsed);
+ krsort($routeParams);
+ $parsed = str_replace(array_keys($routeParams), array_values($routeParams), $parsed);
$this->_compiledRoute = '#^' . $parsed . '[/]*$#';
$this->keys = $names;
}
@@ -2120,6 +2120,21 @@ function testBasicRouteCompiling() {
}
/**
+ * test that route parameters that overlap don't cause errors.
+ *
+ * @return void
+ */
+ function testRouteParameterOverlap() {
+ $route =& new CakeRoute('/invoices/add/:idd/:id', array('controller' => 'invoices', 'action' => 'add'));
+ $result = $route->compile();
+ $this->assertPattern($result, '/invoices/add/1/3');
+
+ $route =& new CakeRoute('/invoices/add/:id/:idd', array('controller' => 'invoices', 'action' => 'add'));
+ $result = $route->compile();
+ $this->assertPattern($result, '/invoices/add/1/3');
+ }
+
+/**
* test compiling routes with keys that have patterns
*
* @return void

0 comments on commit fbaabad

Please sign in to comment.