Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 177dfbc

Browse filesBrowse files
[Routing] fix matching trailing vars with defaults
1 parent cc497a5 commit 177dfbc
Copy full SHA for 177dfbc

File tree

3 files changed

+13
-2
lines changed
Filter options

3 files changed

+13
-2
lines changed

‎src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherTrait.php
+1-1Lines 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 {

‎src/Symfony/Component/Routing/Matcher/UrlMatcher.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Matcher/UrlMatcher.php
+1-1Lines 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 {

‎src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php
+11Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.