bug #31207 [Routing] Fixed unexpected 404 NoConfigurationException (yceruto)

This PR was merged into the 4.2 branch.

Discussion
----------

[Routing] Fixed unexpected 404 NoConfigurationException

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #31199
| License       | MIT

This is the patch for 4.2+
We need a different patch for 3.4 that is more complex, I think.

Commits
-------

aa71a42a49 [Routing] Fixed unexpected 404 NoConfigurationException
This commit is contained in:
Nicolas Grekas 2019-05-09 11:06:20 +02:00
commit 77684e44dd
3 changed files with 16 additions and 27 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -727,6 +727,7 @@ class UrlMatcherTest extends TestCase
/**
* @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
* @expectedExceptionMessage No routes found for "/".
*/
public function testSchemeAndMethodMismatch()
{