diff --git a/.gitignore b/.gitignore index 002c90b..72f1c0e 100755 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /vendor/ /.idea/ -*.cache \ No newline at end of file +*.cache +.history \ No newline at end of file diff --git a/src/Http/Router.php b/src/Http/Router.php index d332455..8c0e318 100644 --- a/src/Http/Router.php +++ b/src/Http/Router.php @@ -123,7 +123,7 @@ public static function match(string $method, string $path): Route|null return null; } - $parts = array_values(array_filter(explode('/', $path))); + $parts = array_values(array_filter(explode('/', $path), fn($segment) => $segment !== '')); $length = count($parts) - 1; $filteredParams = array_filter(self::$params, fn ($i) => $i <= $length); diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 9aed6a7..a400c59 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -47,6 +47,7 @@ public function testCanMatchUrlWithPlaceholder(): void $this->assertEquals($routeBlogAuthorsComments, Router::match(Http::REQUEST_METHOD_GET, '/blog/authors/comments')); $this->assertEquals($routeBlogPost, Router::match(Http::REQUEST_METHOD_GET, '/blog/test')); $this->assertEquals($routeBlogPostComments, Router::match(Http::REQUEST_METHOD_GET, '/blog/test/comments')); + $this->assertEquals($routeBlogPostCommentsSingle, Router::match(Http::REQUEST_METHOD_GET, '/blog/test/comments/0')); $this->assertEquals($routeBlogPostCommentsSingle, Router::match(Http::REQUEST_METHOD_GET, '/blog/test/comments/:comment')); }