Skip to content

Commit ae064a6

Browse files
cs278nicolas-grekas
authored andcommitted
[Routing] Fix matching the "0" URL
1 parent 6fc4c44 commit ae064a6

File tree

5 files changed

+19
-7
lines changed

5 files changed

+19
-7
lines changed

Matcher/Dumper/CompiledUrlMatcherTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function match(string $pathinfo): array
5757
} finally {
5858
$this->context->setScheme($scheme);
5959
}
60-
} elseif ('/' !== $trimmedPathinfo = rtrim($pathinfo, '/') ?: '/') {
60+
} elseif ('' !== $trimmedPathinfo = rtrim($pathinfo, '/')) {
6161
$pathinfo = $trimmedPathinfo === $pathinfo ? $pathinfo.'/' : $trimmedPathinfo;
6262
if ($ret = $this->doMatch($pathinfo, $allow, $allowSchemes)) {
6363
return $this->redirect($pathinfo, $ret['_route']) + $ret;
@@ -73,8 +73,8 @@ public function match(string $pathinfo): array
7373
private function doMatch(string $pathinfo, array &$allow = [], array &$allowSchemes = []): array
7474
{
7575
$allow = $allowSchemes = [];
76-
$pathinfo = rawurldecode($pathinfo) ?: '/';
77-
$trimmedPathinfo = rtrim($pathinfo, '/') ?: '/';
76+
$pathinfo = '' === ($pathinfo = rawurldecode($pathinfo)) ? '/' : $pathinfo;
77+
$trimmedPathinfo = '' === ($trimmedPathinfo = rtrim($pathinfo, '/')) ? '/' : $trimmedPathinfo;
7878
$context = $this->context;
7979
$requestMethod = $canonicalMethod = $context->getMethod();
8080

Matcher/RedirectableUrlMatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function match(string $pathinfo): array
4141
} finally {
4242
$this->context->setScheme($scheme);
4343
}
44-
} elseif ('/' === $trimmedPathinfo = rtrim($pathinfo, '/') ?: '/') {
44+
} elseif ('' === $trimmedPathinfo = rtrim($pathinfo, '/')) {
4545
throw $e;
4646
} else {
4747
try {

Matcher/TraceableUrlMatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected function matchCollection(string $pathinfo, RouteCollection $routes): a
6363
$method = 'GET';
6464
}
6565
$supportsTrailingSlash = 'GET' === $method && $this instanceof RedirectableUrlMatcherInterface;
66-
$trimmedPathinfo = rtrim($pathinfo, '/') ?: '/';
66+
$trimmedPathinfo = '' === ($trimmedPathinfo = rtrim($pathinfo, '/')) ? '/' : $trimmedPathinfo;
6767

6868
foreach ($routes as $name => $route) {
6969
$compiledRoute = $route->compile();

Matcher/UrlMatcher.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ public function getContext(): RequestContext
7878
public function match(string $pathinfo): array
7979
{
8080
$this->allow = $this->allowSchemes = [];
81+
$pathinfo = '' === ($pathinfo = rawurldecode($pathinfo)) ? '/' : $pathinfo;
8182

82-
if ($ret = $this->matchCollection(rawurldecode($pathinfo) ?: '/', $this->routes)) {
83+
if ($ret = $this->matchCollection($pathinfo, $this->routes)) {
8384
return $ret;
8485
}
8586

@@ -125,7 +126,7 @@ protected function matchCollection(string $pathinfo, RouteCollection $routes): a
125126
$method = 'GET';
126127
}
127128
$supportsTrailingSlash = 'GET' === $method && $this instanceof RedirectableUrlMatcherInterface;
128-
$trimmedPathinfo = rtrim($pathinfo, '/') ?: '/';
129+
$trimmedPathinfo = '' === ($trimmedPathinfo = rtrim($pathinfo, '/')) ? '/' : $trimmedPathinfo;
129130

130131
foreach ($routes as $name => $route) {
131132
$compiledRoute = $route->compile();

Tests/Matcher/UrlMatcherTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@
2222

2323
class UrlMatcherTest extends TestCase
2424
{
25+
public function testZero()
26+
{
27+
$coll = new RouteCollection();
28+
$coll->add('index', new Route('/'));
29+
30+
$matcher = $this->getUrlMatcher($coll);
31+
32+
$this->expectException(ResourceNotFoundException::class);
33+
$matcher->match('0');
34+
}
35+
2536
public function testNoMethodSoAllowed()
2637
{
2738
$coll = new RouteCollection();

0 commit comments

Comments
 (0)