From 45eabf2503a04c12d7c9cb53989f8f5594a70d12 Mon Sep 17 00:00:00 2001 From: clfreville2 Date: Tue, 22 Nov 2022 09:50:47 +0100 Subject: [PATCH] Utilise le chemin de base plus tard dans le routeur --- src/Silex/Router/Router.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Silex/Router/Router.php b/src/Silex/Router/Router.php index 23c38e0..3a111b4 100644 --- a/src/Silex/Router/Router.php +++ b/src/Silex/Router/Router.php @@ -27,9 +27,6 @@ class Router public function setBasePath(string $basePath) { - if (PathHelper::startsWith($this->url, $basePath)) { - $this->url = trim(substr($this->url, strlen($basePath)), '/'); - } $this->basePath = $basePath; } @@ -67,11 +64,17 @@ class Router if (!isset($this->routes[$_SERVER['REQUEST_METHOD']])) { throw new RouteNotFoundException('Unknown HTTP method'); } - if ($this->basePath === '' || PathHelper::startsWith($this->url, $this->basePath)) { - foreach ($this->routes[$_SERVER['REQUEST_METHOD']] as $route) { - if ($route->matches($this->url)) { - return $route->call($di); - } + $url = $this->url; + if ($this->basePath !== '') { + if (PathHelper::startsWith($url, $this->basePath)) { + $url = trim(substr($url, strlen($this->basePath)), '/'); + } else { + throw new RouteNotFoundException('No matching routes'); + } + } + foreach ($this->routes[$_SERVER['REQUEST_METHOD']] as $route) { + if ($route->matches($url)) { + return $route->call($di); } } throw new RouteNotFoundException('No matching routes');