diff --git a/src/Routing.php b/src/Routing.php index eb07ca6..7155aca 100644 --- a/src/Routing.php +++ b/src/Routing.php @@ -17,6 +17,8 @@ class Routing { public ResponseInterface $response; + protected array $registeredRoutes; + public function __construct( public Router $router, public RouteFetcher $fetcher, @@ -24,12 +26,20 @@ class Routing { ) { } public function registerRoute(ContainerInterface $container, string $urlBase) { - $this->router->group(rtrim($urlBase, "/"), function (RouteGroup $route) use ($container) { - foreach($this->fetcher->compile() as $attribute) { + $this->registeredRoutes[$urlBase] ??= []; + + $this->router->group(rtrim($urlBase, "/"), function (RouteGroup $route) use ($container, $urlBase) { + foreach(array_reverse($this->fetcher->compile()) as $attribute) { $this->eventManager->execute(Event\RoutingCompileRoutes::class, $this, $attribute); # Mapping every URLs from attributes in searched folders (Api, Controller, etc...) foreach((array) $attribute->method as $method) { + if (! empty($this->registeredRoutes[$urlBase][$attribute->getRoute().$method])) { + continue; + } + + $this->registeredRoutes[$urlBase][$attribute->getRoute().$method] = true; + $route->map(strtoupper($method), $attribute->getRoute(), function (ServerRequestInterface $request, array $arguments) use ( $container, $route, $attribute ) : ResponseInterface {