From aa71a42a492f0e85a16a04edbdb76bfc0846f9c4 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Tue, 23 Apr 2019 09:36:06 -0400 Subject: [PATCH] [Routing] Fixed unexpected 404 NoConfigurationException --- .../Matcher/Dumper/PhpMatcherTrait.php | 22 ++++++++----------- .../Component/Routing/Matcher/UrlMatcher.php | 20 +++++------------ .../Routing/Tests/Matcher/UrlMatcherTest.php | 1 + 3 files changed, 16 insertions(+), 27 deletions(-) diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherTrait.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherTrait.php index 42cd41a915..18b1018773 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherTrait.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherTrait.php @@ -42,7 +42,7 @@ trait PhpMatcherTrait 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 @@ trait PhpMatcherTrait } } - 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 @@ trait PhpMatcherTrait } $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 @@ trait PhpMatcherTrait } } - $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 7b2662a2a3..dca1d6366f 100644 --- a/src/Symfony/Component/Routing/Matcher/UrlMatcher.php +++ b/src/Symfony/Component/Routing/Matcher/UrlMatcher.php @@ -89,7 +89,7 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface return $ret; } - if ('/' === $pathinfo && !$this->allow) { + if ('/' === $pathinfo && !$this->allow && !$this->allowSchemes) { throw new NoConfigurationException(); } @@ -182,24 +182,16 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface 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 ($route->getSchemes() && !$route->hasScheme($this->context->getScheme())) { + $this->allowSchemes = array_merge($this->allowSchemes, $route->getSchemes()); + continue; } - if (!$hasRequiredScheme) { - $this->allowSchemes = array_merge($this->allowSchemes, $route->getSchemes()); - + 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 8f99f7b8e9..823441159a 100644 --- a/src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php +++ b/src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php @@ -727,6 +727,7 @@ class UrlMatcherTest extends TestCase /** * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException + * @expectedExceptionMessage No routes found for "/". */ public function testSchemeAndMethodMismatch() {