[Routing] fix trailing slash redirection with non-greedy trailing vars
This commit is contained in:
parent
dc2edaffe2
commit
d88833d27a
@ -131,6 +131,15 @@ trait PhpMatcherTrait
|
|||||||
}
|
}
|
||||||
|
|
||||||
$hasTrailingVar = $trimmedPathinfo !== $pathinfo && $hasTrailingVar;
|
$hasTrailingVar = $trimmedPathinfo !== $pathinfo && $hasTrailingVar;
|
||||||
|
|
||||||
|
if ($hasTrailingVar && ($hasTrailingSlash || '/' !== substr($matches[\count($vars)], -1)) && preg_match($regex, $this->matchHost ? $host.'.'.$trimmedPathinfo : $trimmedPathinfo, $n) && $m === (int) $n['MARK']) {
|
||||||
|
if ($hasTrailingSlash) {
|
||||||
|
$matches = $n;
|
||||||
|
} else {
|
||||||
|
$hasTrailingVar = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ('/' !== $pathinfo && !$hasTrailingVar && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
|
if ('/' !== $pathinfo && !$hasTrailingVar && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
|
||||||
if ($supportsRedirections && (!$requiredMethods || isset($requiredMethods['GET']))) {
|
if ($supportsRedirections && (!$requiredMethods || isset($requiredMethods['GET']))) {
|
||||||
return $allow = $allowSchemes = [];
|
return $allow = $allowSchemes = [];
|
||||||
@ -138,10 +147,6 @@ trait PhpMatcherTrait
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($hasTrailingSlash && $hasTrailingVar && preg_match($regex, $this->matchHost ? $host.'.'.$trimmedPathinfo : $trimmedPathinfo, $n) && $m === (int) $n['MARK']) {
|
|
||||||
$matches = $n;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($vars as $i => $v) {
|
foreach ($vars as $i => $v) {
|
||||||
if (isset($matches[1 + $i])) {
|
if (isset($matches[1 + $i])) {
|
||||||
$ret[$v] = $matches[1 + $i];
|
$ret[$v] = $matches[1 + $i];
|
||||||
|
@ -158,6 +158,14 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
|
|||||||
|
|
||||||
$hasTrailingVar = $trimmedPathinfo !== $pathinfo && preg_match('#\{\w+\}/?$#', $route->getPath());
|
$hasTrailingVar = $trimmedPathinfo !== $pathinfo && preg_match('#\{\w+\}/?$#', $route->getPath());
|
||||||
|
|
||||||
|
if ($hasTrailingVar && ($hasTrailingSlash || '/' !== substr($matches[(\count($matches) - 1) >> 1], -1)) && preg_match($regex, $trimmedPathinfo, $m)) {
|
||||||
|
if ($hasTrailingSlash) {
|
||||||
|
$matches = $m;
|
||||||
|
} else {
|
||||||
|
$hasTrailingVar = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ('/' !== $pathinfo && !$hasTrailingVar && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
|
if ('/' !== $pathinfo && !$hasTrailingVar && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
|
||||||
if ($supportsTrailingSlash && (!$requiredMethods || \in_array('GET', $requiredMethods))) {
|
if ($supportsTrailingSlash && (!$requiredMethods || \in_array('GET', $requiredMethods))) {
|
||||||
return $this->allow = $this->allowSchemes = [];
|
return $this->allow = $this->allowSchemes = [];
|
||||||
@ -166,10 +174,6 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($hasTrailingSlash && $hasTrailingVar && preg_match($regex, $trimmedPathinfo, $m)) {
|
|
||||||
$matches = $m;
|
|
||||||
}
|
|
||||||
|
|
||||||
$hostMatches = [];
|
$hostMatches = [];
|
||||||
if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
|
if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -187,6 +187,17 @@ class RedirectableUrlMatcherTest extends UrlMatcherTest
|
|||||||
$this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
|
$this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testNonGreedyTrailingRequirement()
|
||||||
|
{
|
||||||
|
$coll = new RouteCollection();
|
||||||
|
$coll->add('a', new Route('/{a}', [], ['a' => '\d+']));
|
||||||
|
|
||||||
|
$matcher = $this->getUrlMatcher($coll);
|
||||||
|
$matcher->expects($this->once())->method('redirect')->with('/123')->willReturn([]);
|
||||||
|
|
||||||
|
$this->assertEquals(['_route' => 'a', 'a' => '123'], $matcher->match('/123/'));
|
||||||
|
}
|
||||||
|
|
||||||
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
|
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
|
||||||
{
|
{
|
||||||
return $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', [$routes, $context ?: new RequestContext()]);
|
return $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', [$routes, $context ?: new RequestContext()]);
|
||||||
|
Reference in New Issue
Block a user