Skip to content

Commit 60c659e

Browse files
Merge pull request #142 from ebenezerdon/fix-preserve-numeric-segments
Fix: preserve numeric segments in path matching
2 parents cc880ec + b332eec commit 60c659e

3 files changed

Lines changed: 5 additions & 2 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/vendor/
22
/.idea/
3-
*.cache
3+
*.cache
4+
.history
5+
.phpactor.json

src/Http/Router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public static function match(string $method, string $path): Route|null
123123
return null;
124124
}
125125

126-
$parts = array_values(array_filter(explode('/', $path)));
126+
$parts = array_values(array_filter(explode('/', $path), fn ($segment) => $segment !== ''));
127127
$length = count($parts) - 1;
128128
$filteredParams = array_filter(self::$params, fn ($i) => $i <= $length);
129129

tests/RouterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function testCanMatchUrlWithPlaceholder(): void
4747
$this->assertEquals($routeBlogAuthorsComments, Router::match(Http::REQUEST_METHOD_GET, '/blog/authors/comments'));
4848
$this->assertEquals($routeBlogPost, Router::match(Http::REQUEST_METHOD_GET, '/blog/test'));
4949
$this->assertEquals($routeBlogPostComments, Router::match(Http::REQUEST_METHOD_GET, '/blog/test/comments'));
50+
$this->assertEquals($routeBlogPostCommentsSingle, Router::match(Http::REQUEST_METHOD_GET, '/blog/test/comments/0'));
5051
$this->assertEquals($routeBlogPostCommentsSingle, Router::match(Http::REQUEST_METHOD_GET, '/blog/test/comments/:comment'));
5152
}
5253

0 commit comments

Comments
 (0)