diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherTrait.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherTrait.php index 42cd41a9153c0..18b1018773b1c 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherTrait.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherTrait.php @@ -42,7 +42,7 @@ public function match($pathinfo) throw new MethodNotAllowedException(array_keys($allow)); } if (!$this instanceof RedirectableUrlMatcherInterface) { - throw new ResourceNotFoundException(); + throw new ResourceNotFoundException(sprintf('No routes found for "%s".', $pathinfo)); } if (!\in_array($this->context->getMethod(), ['HEAD', 'GET'], true)) { // no-op @@ -67,7 +67,7 @@ public function match($pathinfo) } } - throw new ResourceNotFoundException(); + throw new ResourceNotFoundException(sprintf('No routes found for "%s".', $pathinfo)); } private function doMatch(string $pathinfo, array &$allow = [], array &$allowSchemes = []): array @@ -110,10 +110,8 @@ private function doMatch(string $pathinfo, array &$allow = [], array &$allowSche } $hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]); - if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) { - if ($hasRequiredScheme) { - $allow += $requiredMethods; - } + if ($hasRequiredScheme && $requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) { + $allow += $requiredMethods; continue; } @@ -157,15 +155,13 @@ private function doMatch(string $pathinfo, array &$allow = [], array &$allowSche } } - $hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]); - if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) { - if ($hasRequiredScheme) { - $allow += $requiredMethods; - } + if ($requiredSchemes && !isset($requiredSchemes[$context->getScheme()])) { + $allowSchemes += $requiredSchemes; continue; } - if (!$hasRequiredScheme) { - $allowSchemes += $requiredSchemes; + + if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) { + $allow += $requiredMethods; continue; } diff --git a/src/Symfony/Component/Routing/Matcher/UrlMatcher.php b/src/Symfony/Component/Routing/Matcher/UrlMatcher.php index 7b2662a2a392e..dca1d6366fa02 100644 --- a/src/Symfony/Component/Routing/Matcher/UrlMatcher.php +++ b/src/Symfony/Component/Routing/Matcher/UrlMatcher.php @@ -89,7 +89,7 @@ public function match($pathinfo) return $ret; } - if ('/' === $pathinfo && !$this->allow) { + if ('/' === $pathinfo && !$this->allow && !$this->allowSchemes) { throw new NoConfigurationException(); } @@ -182,24 +182,16 @@ protected function matchCollection($pathinfo, RouteCollection $routes) if ($supportsTrailingSlash && (!$requiredMethods || \in_array('GET', $requiredMethods))) { return $this->allow = $this->allowSchemes = []; } - continue; } - $hasRequiredScheme = !$route->getSchemes() || $route->hasScheme($this->context->getScheme()); - if ($requiredMethods) { - if (!\in_array($method, $requiredMethods)) { - if ($hasRequiredScheme) { - $this->allow = array_merge($this->allow, $requiredMethods); - } - - continue; - } - } - - if (!$hasRequiredScheme) { + if ($route->getSchemes() && !$route->hasScheme($this->context->getScheme())) { $this->allowSchemes = array_merge($this->allowSchemes, $route->getSchemes()); + continue; + } + if ($requiredMethods && !\in_array($method, $requiredMethods)) { + $this->allow = array_merge($this->allow, $requiredMethods); continue; } diff --git a/src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php b/src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php index 8f99f7b8e9988..823441159a87b 100644 --- a/src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php +++ b/src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php @@ -727,6 +727,7 @@ public function testNestedCollections() /** * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException + * @expectedExceptionMessage No routes found for "/". */ public function testSchemeAndMethodMismatch() {