From e7ac4f5fa03869580600f221ab1264eb4e808d4f Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Fri, 14 Feb 2020 15:18:56 +0100 Subject: [PATCH] Move fallback routes to the end of symfony routes collection --- .../Routing/AbstractRouteCollection.php | 44 +++++++++++++++---- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Routing/AbstractRouteCollection.php b/src/Illuminate/Routing/AbstractRouteCollection.php index 867f8084255a..4d5f44838588 100644 --- a/src/Illuminate/Routing/AbstractRouteCollection.php +++ b/src/Illuminate/Routing/AbstractRouteCollection.php @@ -166,20 +166,46 @@ public function dumper() public function toSymfonyRouteCollection() { $symfonyRoutes = new SymfonyRouteCollection(); + $routes = $this->getRoutes(); + + // Because we need to make sure that fallback routes are always placed as last + // we'll first iterate over the regular routes and add them to the symfony + // routes collection. Then we'll do the same thing for the fallback routes. + foreach ($routes as $route) { + if (! $route->isFallback) { + $symfonyRoutes = $this->addToSymfonyRoutesCollection($symfonyRoutes, $route); + } + } - foreach ($this->getRoutes() as $route) { - // If the route doesn't have a name, we'll generate one for it - // and re-add the route to the collection. This way we can - // add the route to the Symfony route collection. - if (! $name = $route->getName()) { - $route->name($name = $this->generateRouteName()); - - $this->add($route); + foreach ($routes as $route) { + if ($route->isFallback) { + $symfonyRoutes = $this->addToSymfonyRoutesCollection($symfonyRoutes, $route); } + } + + return $symfonyRoutes; + } - $symfonyRoutes->add($name, $route->toSymfonyRoute()); + /** + * Add a route to the SymfonyRouteCollection instance. + * + * @param \Symfony\Component\Routing\RouteCollection $symfonyRoutes + * @param \Illuminate\Routing\Route $route + * @return \Symfony\Component\Routing\RouteCollection + */ + protected function addToSymfonyRoutesCollection(SymfonyRouteCollection $symfonyRoutes, Route $route) + { + // If the route doesn't have a name, we'll generate one for it + // and re-add the route to the collection. This way we can + // add the route to the Symfony route collection. + if (! $name = $route->getName()) { + $route->name($name = $this->generateRouteName()); + + $this->add($route); } + $symfonyRoutes->add($name, $route->toSymfonyRoute()); + return $symfonyRoutes; }