Skip to content

Commit 9d186b9

Browse files
[Routing] fix matching trailing vars with defaults
1 parent 0e5719d commit 9d186b9

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

Matcher/Dumper/PhpMatcherTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private function doMatch(string $pathinfo, array &$allow = [], array &$allowSche
132132

133133
$hasTrailingVar = $trimmedPathinfo !== $pathinfo && $hasTrailingVar;
134134

135-
if ($hasTrailingVar && ($hasTrailingSlash || '/' !== substr($matches[\count($vars)], -1)) && preg_match($regex, $this->matchHost ? $host.'.'.$trimmedPathinfo : $trimmedPathinfo, $n) && $m === (int) $n['MARK']) {
135+
if ($hasTrailingVar && ($hasTrailingSlash || !($n = $matches[\count($vars)] ?? '') || '/' !== substr($n, -1)) && preg_match($regex, $this->matchHost ? $host.'.'.$trimmedPathinfo : $trimmedPathinfo, $n) && $m === (int) $n['MARK']) {
136136
if ($hasTrailingSlash) {
137137
$matches = $n;
138138
} else {

Matcher/UrlMatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
158158

159159
$hasTrailingVar = $trimmedPathinfo !== $pathinfo && preg_match('#\{\w+\}/?$#', $route->getPath());
160160

161-
if ($hasTrailingVar && ($hasTrailingSlash || '/' !== substr($matches[(\count($matches) - 1) >> 1], -1)) && preg_match($regex, $trimmedPathinfo, $m)) {
161+
if ($hasTrailingVar && ($hasTrailingSlash || !($m = $matches[\count($compiledRoute->getPathVariables())] ?? '') || '/' !== substr($m, -1)) && preg_match($regex, $trimmedPathinfo, $m)) {
162162
if ($hasTrailingSlash) {
163163
$matches = $m;
164164
} else {

Tests/Matcher/RedirectableUrlMatcherTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,17 @@ public function testNonGreedyTrailingRequirement()
198198
$this->assertEquals(['_route' => 'a', 'a' => '123'], $matcher->match('/123/'));
199199
}
200200

201+
public function testTrailingRequirementWithDefault()
202+
{
203+
$coll = new RouteCollection();
204+
$coll->add('a', new Route('/foo/{a}', ['a' => 'bar'], ['a' => '.+']));
205+
206+
$matcher = $this->getUrlMatcher($coll);
207+
$matcher->expects($this->once())->method('redirect')->with('/foo')->willReturn([]);
208+
209+
$this->assertEquals(['_route' => 'a', 'a' => 'bar'], $matcher->match('/foo/'));
210+
}
211+
201212
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
202213
{
203214
return $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', [$routes, $context ?: new RequestContext()]);

0 commit comments

Comments
 (0)