Skip to content
Permalink
Browse files

Fix error where routes would be incorrectly parsed.

Instead of only trying the first path partition, we should check each
matching prefix slice until all path prefixes have been exhausted.

Fixes #3954
  • Loading branch information...
markstory committed Jul 13, 2014
1 parent 6b796b7 commit 31f656b6c1e6b3c908e8ac33c136f5111b4b3d4f
Showing with 37 additions and 15 deletions.
  1. +15 −15 src/Routing/RouteCollection.php
  2. +22 −0 tests/TestCase/Routing/RouteCollectionTest.php
@@ -93,25 +93,25 @@ public function add(Route $route, $options = []) {
*/
public function parse($url) {
foreach (array_keys($this->_paths) as $path) {
if (strpos($url, $path) === 0) {
break;
if (strpos($url, $path) !== 0) {
continue;
}
}
$queryParameters = null;
if (strpos($url, '?') !== false) {
list($url, $queryParameters) = explode('?', $url, 2);
parse_str($queryParameters, $queryParameters);
}
foreach ($this->_paths[$path] as $route) {
$r = $route->parse($url);
if ($r === false) {
continue;
$queryParameters = null;
if (strpos($url, '?') !== false) {
list($url, $queryParameters) = explode('?', $url, 2);
parse_str($queryParameters, $queryParameters);
}
if ($queryParameters) {
$r['?'] = $queryParameters;
foreach ($this->_paths[$path] as $route) {
$r = $route->parse($url);
if ($r === false) {
continue;
}
if ($queryParameters) {
$r['?'] = $queryParameters;
}
return $r;
}
return $r;
}
throw new MissingRouteException(['url' => $url]);
}
@@ -101,6 +101,28 @@ public function testParse() {
$this->assertEquals($expected, $result);
}
/**
* Test that parsing checks all the related path scopes.
*
* @return void
*/
public function testParseFallback() {
$routes = new RouteBuilder($this->collection, '/', []);
$routes->resources('Articles');
$routes->connect('/:controller', ['action' => 'index'], [], ['routeClass' => 'InflectedRoute']);
$routes->connect('/:controller/:action', [], ['routeClass' => 'InflectedRoute']);
$result = $this->collection->parse('/articles/add');
$expected = [
'controller' => 'Articles',
'action' => 'add',
'plugin' => null,
'pass' => []
];
$this->assertEquals($expected, $result);
}
/**
* Test match() throws an error on unknown routes.
*

0 comments on commit 31f656b

Please sign in to comment.
You can’t perform that action at this time.