From 2a5d11b40392748f86c64335b96fba0bd18fbd51 Mon Sep 17 00:00:00 2001 From: Josias Montag Date: Fri, 24 Mar 2017 15:16:57 +0100 Subject: [PATCH 1/4] Add test for overwriting routes --- tests/Routing/RouteCollectionTest.php | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/Routing/RouteCollectionTest.php b/tests/Routing/RouteCollectionTest.php index b0119cd42d83..6bb420a30577 100644 --- a/tests/Routing/RouteCollectionTest.php +++ b/tests/Routing/RouteCollectionTest.php @@ -165,4 +165,35 @@ 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')); + + } + + } From 5c59eb2df7f129453b671e1883a24e994e53d727 Mon Sep 17 00:00:00 2001 From: Josias Montag Date: Fri, 24 Mar 2017 15:17:32 +0100 Subject: [PATCH 2/4] Method to refresh the action look-up table in RouteCollection --- src/Illuminate/Routing/RouteCollection.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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. * From 871914955ae078f4d63d148a2dc719a8cecccdab Mon Sep 17 00:00:00 2001 From: Josias Montag Date: Fri, 24 Mar 2017 15:18:02 +0100 Subject: [PATCH 3/4] Refresh the lookup tables before caching routes --- src/Illuminate/Foundation/Console/RouteCacheCommand.php | 3 +++ .../Foundation/Support/Providers/RouteServiceProvider.php | 1 + 2 files changed, 4 insertions(+) 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(); }); } } From 81b46d888f4c080adb848a93fc419c0b4d5bf634 Mon Sep 17 00:00:00 2001 From: Josias Montag Date: Fri, 24 Mar 2017 15:43:54 +0100 Subject: [PATCH 4/4] Style fix --- tests/Routing/RouteCollectionTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/Routing/RouteCollectionTest.php b/tests/Routing/RouteCollectionTest.php index 6bb420a30577..7b3692154c23 100644 --- a/tests/Routing/RouteCollectionTest.php +++ b/tests/Routing/RouteCollectionTest.php @@ -166,7 +166,6 @@ public function testRouteCollectionCanGetAllRoutes() $this->assertEquals($allRoutes, $this->routeCollection->getRoutes()); } - public function testRouteCollectionCleansUpOverwrittenRoutes() { // Create two routes with the same path and method. @@ -192,8 +191,5 @@ public function testRouteCollectionCleansUpOverwrittenRoutes() // The lookups of $routeB are still there. $this->assertEquals($routeB, $this->routeCollection->getByName('overwrittenRouteA')); $this->assertEquals($routeB, $this->routeCollection->getByAction('OverwrittenView@view')); - } - - }