Skip to content

Commit

Permalink
Fix Router::promote() & named route table.
Browse files Browse the repository at this point in the history
Previously promote() only modified the linear list.  It should also
update the routes matching slice of the route table.
  • Loading branch information
markstory committed Jul 13, 2012
1 parent 45ff4a0 commit 1f164f0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
13 changes: 12 additions & 1 deletion lib/Cake/Routing/RouteCollection.php
Expand Up @@ -175,7 +175,8 @@ public function parse($url) {

/**
* Promote a route (by default, the last one added) to the beginning of the list.
* Does not modify route ordering stored in the hashtable lookups.
* Also promotes the route to the head of its named slice in the route
* hashtable.
*
* @param integer $which A zero-based array index representing
* the route to move. For example,
Expand All @@ -193,6 +194,16 @@ public function promote($which) {
$route =& $this->_routes[$which];
unset($this->_routes[$which]);
array_unshift($this->_routes, $route);

$name = $route->getName();
$routes = $this->_routeTable[$name];
foreach ($routes as $i => $otherRoute) {
if ($route == $otherRoute) {
$index = $i;

This comment has been minimized.

Copy link
@biesbjerg

biesbjerg Jul 13, 2012

Might be faster (and cleaner) to use $index = array_search($route, $routes, true); instead?

This comment has been minimized.

Copy link
@markstory

markstory Jul 14, 2012

Author Member

Good point, updated in [a08f4e7]

}
}
unset($this->_routeTable[$name][$index]);
array_unshift($this->_routeTable[$name], $route);
return true;
}

Expand Down
4 changes: 0 additions & 4 deletions lib/Cake/Routing/Router.php
Expand Up @@ -430,16 +430,12 @@ public static function prefixes() {
* @return array Parsed elements from URL
*/
public static function parse($url) {
$ext = null;
$out = array();

if ($url && strpos($url, '/') !== 0) {
$url = '/' . $url;
}
if (strpos($url, '?') !== false) {
$url = substr($url, 0, strpos($url, '?'));
}

return static::$_routes->parse($url);
}

Expand Down
18 changes: 18 additions & 0 deletions lib/Cake/Test/TestCase/Routing/RouterTest.php
Expand Up @@ -2322,4 +2322,22 @@ public function testParseNamedParameters() {
$this->assertEquals($expected, $request->params);
}

/**
* Test promote()
*
* @return void
*/
public function testPromote() {
Router::connect('/:controller/:action/*');
Router::connect('/:lang/:controller/:action/*');

$result = Router::url(['controller' => 'posts', 'action' => 'index', 'lang' => 'en']);
$this->assertEquals('/posts/index?lang=en', $result, 'First route should match');

Router::promote();

$result = Router::url(['controller' => 'posts', 'action' => 'index', 'lang' => 'en']);
$this->assertEquals('/en/posts/index', $result, 'promote() should move 2nd route ahead.');
}

}

0 comments on commit 1f164f0

Please sign in to comment.