From d25fe85cedba5eb5e4d2082bf3f873de45e1c0d2 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 3 Jun 2023 19:16:54 +0530 Subject: [PATCH 1/2] feat: add support for trailing slashes in routes and urls --- src/App.php | 8 ++++++++ tests/AppTest.php | 13 +++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/App.php b/src/App.php index 703393d1..f858ec0d 100755 --- a/src/App.php +++ b/src/App.php @@ -502,6 +502,14 @@ public function match(Request $request, bool $fresh = false): ?Route foreach (self::$routes[$method] as $routeUrl => $route) { /** @var Route $route */ + if(str_ends_with($routeUrl, '/') && substr_count($routeUrl, '/') > 1) { + $routeUrl = rtrim($routeUrl, '/'); + } + + if(str_ends_with($url, '/') && substr_count($url, '/') > 1) { + $url = rtrim($url, '/'); + } + // convert urls like '/users/:uid/posts/:pid' to regular expression $regex = '@'.\preg_replace('@:[^/]+@', '([^/]+)', $routeUrl).'@'; diff --git a/tests/AppTest.php b/tests/AppTest.php index 8e325284..f2b5b67e 100755 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -400,6 +400,9 @@ public function providerRouteMatching(): array return [ 'GET request' => [App::REQUEST_METHOD_GET, '/path1'], 'GET request on different route' => [App::REQUEST_METHOD_GET, '/path2'], + 'GET request with trailing slash #1' => [App::REQUEST_METHOD_GET, '/path3', '/path3/'], + 'GET request with trailing slash #2' => [App::REQUEST_METHOD_GET, '/path3/', '/path3/'], + 'GET request with trailing slash #3' => [App::REQUEST_METHOD_GET, '/path3/', '/path3'], 'POST request' => [App::REQUEST_METHOD_POST, '/path1'], 'PUT request' => [App::REQUEST_METHOD_PUT, '/path1'], 'PATCH request' => [App::REQUEST_METHOD_PATCH, '/path1'], @@ -415,9 +418,9 @@ public function providerRouteMatching(): array /** * @dataProvider providerRouteMatching */ - public function testCanMatchRoute(string $method, string $path, string $expected = null): void + public function testCanMatchRoute(string $method, string $path, string $url = null): void { - $expected ??= $path; + $url ??= $path; switch ($method) { case App::REQUEST_METHOD_GET: @@ -438,9 +441,11 @@ public function testCanMatchRoute(string $method, string $path, string $expected } $_SERVER['REQUEST_METHOD'] = $method; - $_SERVER['REQUEST_URI'] = $path; + $_SERVER['REQUEST_URI'] = $url; + + $match = $this->app->match(new Request()); - $this->assertEquals($expected, $this->app->match(new Request())); + $this->assertEquals($expected, $match); $this->assertEquals($expected, $this->app->getRoute()); } From 0dfd196e8a962ad8e4ce5d3ce566e4d6dc126306 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 3 Jun 2023 19:17:43 +0530 Subject: [PATCH 2/2] feat: add support for trailing slashes in routes and urls --- tests/AppTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/AppTest.php b/tests/AppTest.php index f2b5b67e..e2ee73c2 100755 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -443,9 +443,7 @@ public function testCanMatchRoute(string $method, string $path, string $url = nu $_SERVER['REQUEST_METHOD'] = $method; $_SERVER['REQUEST_URI'] = $url; - $match = $this->app->match(new Request()); - - $this->assertEquals($expected, $match); + $this->assertEquals($expected, $this->app->match(new Request())); $this->assertEquals($expected, $this->app->getRoute()); }