Skip to content

Commit

Permalink
Fix error where routes would be incorrectly parsed.
Browse files Browse the repository at this point in the history
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 31f656b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/Routing/RouteCollection.php
Expand Up @@ -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]);
}
Expand Down
22 changes: 22 additions & 0 deletions tests/TestCase/Routing/RouteCollectionTest.php
Expand Up @@ -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.
*
Expand Down

0 comments on commit 31f656b

Please sign in to comment.