Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Fix overwriting of routes #18475

Merged
merged 4 commits into from
Mar 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Illuminate/Foundation/Console/RouteCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public function fire()
return $this->error("Your application doesn't have any routes.");
}

$routes->refreshNameLookups();
$routes->refreshActionLookups();

foreach ($routes as $route) {
$route->prepareForSerialization();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function boot()

$this->app->booted(function () {
$this->app['router']->getRoutes()->refreshNameLookups();
$this->app['router']->getRoutes()->refreshActionLookups();
});
}
}
Expand Down
21 changes: 20 additions & 1 deletion src/Illuminate/Routing/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected function addToActionList($action, $route)
/**
* Refresh the name look-up table.
*
* This is done in case any names are fluently defined.
* This is done in case any names are fluently defined or if routes are overwritten.
*
* @return void
*/
Expand All @@ -128,6 +128,25 @@ public function refreshNameLookups()
}
}

/**
* Refresh the action look-up table.
*
* This is done in case any actions are fluently defined or if routes are overwritten.
*
* @return void
*/
public function refreshActionLookups()
{
$this->actionList = [];

foreach ($this->allRoutes as $route) {
$action = $route->getAction();
if (isset($action['controller'])) {
$this->addToActionList($action, $route);
}
}
}

/**
* Find the first route matching a given request.
*
Expand Down
27 changes: 27 additions & 0 deletions tests/Routing/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,31 @@ public function testRouteCollectionCanGetAllRoutes()
];
$this->assertEquals($allRoutes, $this->routeCollection->getRoutes());
}

public function testRouteCollectionCleansUpOverwrittenRoutes()
{
// Create two routes with the same path and method.
$routeA = new Route('GET', 'product', ['controller' => 'View@view', 'as' => 'routeA']);
$routeB = new Route('GET', 'product', ['controller' => 'OverwrittenView@view', 'as' => 'overwrittenRouteA']);

$this->routeCollection->add($routeA);
$this->routeCollection->add($routeB);

// Check if the lookups of $routeA and $routeB are there.
$this->assertEquals($routeA, $this->routeCollection->getByName('routeA'));
$this->assertEquals($routeA, $this->routeCollection->getByAction('View@view'));
$this->assertEquals($routeB, $this->routeCollection->getByName('overwrittenRouteA'));
$this->assertEquals($routeB, $this->routeCollection->getByAction('OverwrittenView@view'));

// Rebuild the lookup arrays.
$this->routeCollection->refreshNameLookups();
$this->routeCollection->refreshActionLookups();

// The lookups of $routeA should not be there anymore, because they are no longer valid.
$this->assertNull($this->routeCollection->getByName('routeA'));
$this->assertNull($this->routeCollection->getByAction('View@view'));
// The lookups of $routeB are still there.
$this->assertEquals($routeB, $this->routeCollection->getByName('overwrittenRouteA'));
$this->assertEquals($routeB, $this->routeCollection->getByAction('OverwrittenView@view'));
}
}