Skip to content

Commit

Permalink
Raise exceptions when scopes and routes have conflicting parameters
Browse files Browse the repository at this point in the history
Having a scope contain a route with different parameters is most likely
a mistake. In addition it will defeat optimizations for reverse routing.
  • Loading branch information
markstory committed Jun 29, 2014
1 parent 5e72b75 commit 41a97c2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Routing/ScopedRouteCollection.php
Expand Up @@ -390,6 +390,19 @@ protected function _makeRoute($route, $defaults, $options) {

$route = str_replace('//', '/', $this->_path . $route);
if (is_array($defaults)) {
foreach ($this->_params as $param => $val) {
if (isset($defaults[$param]) && $defaults[$param] !== $val) {
$msg = 'You cannot define routes that conflict with the scope. ' .
'Scope had %s = %s, while route had %s = %s';
throw new Error\Exception(sprintf(
$msg,
$param,
$val,
$param,
$defaults[$param]
));
}
}
$defaults += $this->_params;
}
$route = new $routeClass($route, $defaults, $options);
Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/Routing/ScopedRouteCollectionTest.php
Expand Up @@ -166,6 +166,18 @@ public function testConnectErrorInvalidRouteClass() {
$routes->connect('/:controller', [], ['routeClass' => '\StdClass']);
}

/**
* Test conflicting parameters raises an exception.
*
* @expectedException \Cake\Error\Exception
* @expectedExceptionMessage You cannot define routes that conflict with the scope.
* @return void
*/
public function testConnectConflictingParameters() {
$routes = new ScopedRouteCollection('/admin', ['prefix' => 'admin'], []);
$routes->connect('/', ['prefix' => 'manager', 'controller' => 'Dashboard', 'action' => 'view']);
}

/**
* Test connecting redirect routes.
*
Expand Down

0 comments on commit 41a97c2

Please sign in to comment.