[Routing] fix matching trailing vars with defaults
This commit is contained in:
parent
cc497a57c5
commit
177dfbc170
@ -132,7 +132,7 @@ 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 ($hasTrailingVar && ($hasTrailingSlash || !($n = $matches[\count($vars)] ?? '') || '/' !== substr($n, -1)) && preg_match($regex, $this->matchHost ? $host.'.'.$trimmedPathinfo : $trimmedPathinfo, $n) && $m === (int) $n['MARK']) {
|
||||||
if ($hasTrailingSlash) {
|
if ($hasTrailingSlash) {
|
||||||
$matches = $n;
|
$matches = $n;
|
||||||
} else {
|
} else {
|
||||||
|
@ -158,7 +158,7 @@ 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 ($hasTrailingVar && ($hasTrailingSlash || !($m = $matches[\count($compiledRoute->getPathVariables())] ?? '') || '/' !== substr($m, -1)) && preg_match($regex, $trimmedPathinfo, $m)) {
|
||||||
if ($hasTrailingSlash) {
|
if ($hasTrailingSlash) {
|
||||||
$matches = $m;
|
$matches = $m;
|
||||||
} else {
|
} else {
|
||||||
|
@ -198,6 +198,17 @@ class RedirectableUrlMatcherTest extends UrlMatcherTest
|
|||||||
$this->assertEquals(['_route' => 'a', 'a' => '123'], $matcher->match('/123/'));
|
$this->assertEquals(['_route' => 'a', 'a' => '123'], $matcher->match('/123/'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testTrailingRequirementWithDefault()
|
||||||
|
{
|
||||||
|
$coll = new RouteCollection();
|
||||||
|
$coll->add('a', new Route('/foo/{a}', ['a' => 'bar'], ['a' => '.+']));
|
||||||
|
|
||||||
|
$matcher = $this->getUrlMatcher($coll);
|
||||||
|
$matcher->expects($this->once())->method('redirect')->with('/foo')->willReturn([]);
|
||||||
|
|
||||||
|
$this->assertEquals(['_route' => 'a', 'a' => 'bar'], $matcher->match('/foo/'));
|
||||||
|
}
|
||||||
|
|
||||||
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