From 86827d1983d51e80a1d2075fa0310d958a078290 Mon Sep 17 00:00:00 2001 From: clfreville2 Date: Tue, 22 Nov 2022 08:40:05 +0100 Subject: [PATCH] =?UTF-8?q?G=C3=A8re=20davantage=20de=20cas=20dans=20le=20?= =?UTF-8?q?routeur?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Silex/Router/PathHelper.php | 17 +++++++++++++++++ src/Silex/Router/Router.php | 19 ++++++++++++------- 2 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 src/Silex/Router/PathHelper.php diff --git a/src/Silex/Router/PathHelper.php b/src/Silex/Router/PathHelper.php new file mode 100644 index 0000000..820d1bb --- /dev/null +++ b/src/Silex/Router/PathHelper.php @@ -0,0 +1,17 @@ +url = trim($url, '/');; + $url = PathHelper::removeEverythingAfter($url, '?'); + $url = PathHelper::removeEverythingAfter($url, '#'); + $this->url = trim($url, '/'); } public function setBasePath(string $basePath) { $pos = strpos($this->url, $basePath); - if ($pos !== false) { - $this->url = substr($this->url, $pos + strlen($basePath)); + if ($pos === false) { + $this->url = null; + } else { + $this->url = trim(substr($this->url, $pos + strlen($basePath)), '/'); } - var_dump($this->url); } public function get(string $path, callable $callable): self @@ -48,9 +51,11 @@ class Router if (!isset($this->routes[$_SERVER['REQUEST_METHOD']])) { throw new RouteNotFoundException('Unknown HTTP method'); } - foreach ($this->routes[$_SERVER['REQUEST_METHOD']] as $route) { - if ($route->matches($this->url)) { - return $route->call($di); + if ($this->url !== null) { + foreach ($this->routes[$_SERVER['REQUEST_METHOD']] as $route) { + if ($route->matches($this->url)) { + return $route->call($di); + } } } throw new RouteNotFoundException('No matching routes');