diff --git a/src/Illuminate/Foundation/Console/RouteCacheCommand.php b/src/Illuminate/Foundation/Console/RouteCacheCommand.php index e139e0a0480e..dcb90ac7ce3b 100644 --- a/src/Illuminate/Foundation/Console/RouteCacheCommand.php +++ b/src/Illuminate/Foundation/Console/RouteCacheCommand.php @@ -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(); } diff --git a/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php b/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php index 564705a62080..8cdd3fa28f17 100644 --- a/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php +++ b/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php @@ -31,6 +31,7 @@ public function boot() $this->app->booted(function () { $this->app['router']->getRoutes()->refreshNameLookups(); + $this->app['router']->getRoutes()->refreshActionLookups(); }); } } diff --git a/src/Illuminate/Routing/RouteCollection.php b/src/Illuminate/Routing/RouteCollection.php index b6317def6dbf..9de51c2f0a39 100644 --- a/src/Illuminate/Routing/RouteCollection.php +++ b/src/Illuminate/Routing/RouteCollection.php @@ -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 */ @@ -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. * diff --git a/tests/Routing/RouteCollectionTest.php b/tests/Routing/RouteCollectionTest.php index b0119cd42d83..7b3692154c23 100644 --- a/tests/Routing/RouteCollectionTest.php +++ b/tests/Routing/RouteCollectionTest.php @@ -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')); + } }