Skip to content

Commit

Permalink
Merge branch 'fix-route-overwrite' of https://github.com/josiasmontag…
Browse files Browse the repository at this point in the history
…/framework into josiasmontag-fix-route-overwrite
  • Loading branch information
taylorotwell committed Mar 24, 2017
2 parents 4a6dcef + 81b46d8 commit e9b3c18
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
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'));
}
}

0 comments on commit e9b3c18

Please sign in to comment.