Skip to content

Commit 221b839

Browse files
ycerutonicolas-grekas
authored andcommitted
[Routing] Fixed unexpected 404 NoConfigurationException
1 parent f4e43bb commit 221b839

File tree

3 files changed

+16
-27
lines changed

3 files changed

+16
-27
lines changed

Matcher/Dumper/PhpMatcherTrait.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function match($pathinfo)
4242
throw new MethodNotAllowedException(array_keys($allow));
4343
}
4444
if (!$this instanceof RedirectableUrlMatcherInterface) {
45-
throw new ResourceNotFoundException();
45+
throw new ResourceNotFoundException(sprintf('No routes found for "%s".', $pathinfo));
4646
}
4747
if (!\in_array($this->context->getMethod(), ['HEAD', 'GET'], true)) {
4848
// no-op
@@ -67,7 +67,7 @@ public function match($pathinfo)
6767
}
6868
}
6969

70-
throw new ResourceNotFoundException();
70+
throw new ResourceNotFoundException(sprintf('No routes found for "%s".', $pathinfo));
7171
}
7272

7373
private function doMatch(string $pathinfo, array &$allow = [], array &$allowSchemes = []): array
@@ -110,10 +110,8 @@ private function doMatch(string $pathinfo, array &$allow = [], array &$allowSche
110110
}
111111

112112
$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
113-
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
114-
if ($hasRequiredScheme) {
115-
$allow += $requiredMethods;
116-
}
113+
if ($hasRequiredScheme && $requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
114+
$allow += $requiredMethods;
117115
continue;
118116
}
119117

@@ -157,15 +155,13 @@ private function doMatch(string $pathinfo, array &$allow = [], array &$allowSche
157155
}
158156
}
159157

160-
$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
161-
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
162-
if ($hasRequiredScheme) {
163-
$allow += $requiredMethods;
164-
}
158+
if ($requiredSchemes && !isset($requiredSchemes[$context->getScheme()])) {
159+
$allowSchemes += $requiredSchemes;
165160
continue;
166161
}
167-
if (!$hasRequiredScheme) {
168-
$allowSchemes += $requiredSchemes;
162+
163+
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
164+
$allow += $requiredMethods;
169165
continue;
170166
}
171167

Matcher/UrlMatcher.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function match($pathinfo)
8989
return $ret;
9090
}
9191

92-
if ('/' === $pathinfo && !$this->allow) {
92+
if ('/' === $pathinfo && !$this->allow && !$this->allowSchemes) {
9393
throw new NoConfigurationException();
9494
}
9595

@@ -182,24 +182,16 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
182182
if ($supportsTrailingSlash && (!$requiredMethods || \in_array('GET', $requiredMethods))) {
183183
return $this->allow = $this->allowSchemes = [];
184184
}
185-
186185
continue;
187186
}
188187

189-
$hasRequiredScheme = !$route->getSchemes() || $route->hasScheme($this->context->getScheme());
190-
if ($requiredMethods) {
191-
if (!\in_array($method, $requiredMethods)) {
192-
if ($hasRequiredScheme) {
193-
$this->allow = array_merge($this->allow, $requiredMethods);
194-
}
195-
196-
continue;
197-
}
198-
}
199-
200-
if (!$hasRequiredScheme) {
188+
if ($route->getSchemes() && !$route->hasScheme($this->context->getScheme())) {
201189
$this->allowSchemes = array_merge($this->allowSchemes, $route->getSchemes());
190+
continue;
191+
}
202192

193+
if ($requiredMethods && !\in_array($method, $requiredMethods)) {
194+
$this->allow = array_merge($this->allow, $requiredMethods);
203195
continue;
204196
}
205197

Tests/Matcher/UrlMatcherTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,7 @@ public function testNestedCollections()
727727

728728
/**
729729
* @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
730+
* @expectedExceptionMessage No routes found for "/".
730731
*/
731732
public function testSchemeAndMethodMismatch()
732733
{

0 commit comments

Comments
 (0)