From 427426bebea65ecc202773ad33d5fba35a4b22e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 16 Apr 2023 20:33:22 +0200 Subject: [PATCH] unify url handling for routes and redirect --- src/Response/Redirect.php | 2 +- src/Routing/Route.php | 2 +- src/Routing/RouteCollection.php | 7 ++++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Response/Redirect.php b/src/Response/Redirect.php index d17b48b..5cc4994 100644 --- a/src/Response/Redirect.php +++ b/src/Response/Redirect.php @@ -19,7 +19,7 @@ class Redirect implements IRedirect if (preg_match('/^http(s)?/', $this->target) === 1) { $link = $this->target; } else { - $link = \Container::$request->getBase() . '/' . $this->target; + $link = \Container::$request->getBase() . $this->target; } return $link; diff --git a/src/Routing/Route.php b/src/Routing/Route.php index ad80cce..fe40344 100644 --- a/src/Routing/Route.php +++ b/src/Routing/Route.php @@ -53,7 +53,7 @@ class Route $query = count($queryParams) > 0 ? '?' . http_build_query($queryParams) : ''; - return implode('/', $link) . $query; + return '/' . implode('/', $link) . $query; } public function testAgainst(array $path): ?array diff --git a/src/Routing/RouteCollection.php b/src/Routing/RouteCollection.php index a554fdb..32fadf6 100644 --- a/src/Routing/RouteCollection.php +++ b/src/Routing/RouteCollection.php @@ -41,9 +41,10 @@ class RouteCollection return $this->routes[$id]; } - public function match(string $method, array $uri): ?array + public function match(string $method, string $uri): ?array { - $groupNumber = count($uri); + $path = $uri === '/' ? [] : explode('/', ltrim($uri, '/')); + $groupNumber = count($path); // response to HEAD request with the GET content if ($method === 'head') { @@ -55,7 +56,7 @@ class RouteCollection } foreach ($this->searchTable[$method][$groupNumber] as $route) { - if (($parameters = $route->testAgainst($uri)) !== null) { + if (($parameters = $route->testAgainst($path)) !== null) { return [$route, $parameters]; } }