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 d88833d

Browse filesBrowse files
[Routing] fix trailing slash redirection with non-greedy trailing vars
1 parent dc2edaf commit d88833d
Copy full SHA for d88833d

File tree

3 files changed

+28
-8
lines changed
Filter options

3 files changed

+28
-8
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
+9-4Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,22 @@ private function doMatch(string $pathinfo, array &$allow = [], array &$allowSche
131131
}
132132

133133
$hasTrailingVar = $trimmedPathinfo !== $pathinfo && $hasTrailingVar;
134+
135+
if ($hasTrailingVar && ($hasTrailingSlash || '/' !== substr($matches[\count($vars)], -1)) && preg_match($regex, $this->matchHost ? $host.'.'.$trimmedPathinfo : $trimmedPathinfo, $n) && $m === (int) $n['MARK']) {
136+
if ($hasTrailingSlash) {
137+
$matches = $n;
138+
} else {
139+
$hasTrailingVar = false;
140+
}
141+
}
142+
134143
if ('/' !== $pathinfo && !$hasTrailingVar && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
135144
if ($supportsRedirections && (!$requiredMethods || isset($requiredMethods['GET']))) {
136145
return $allow = $allowSchemes = [];
137146
}
138147
continue;
139148
}
140149

141-
if ($hasTrailingSlash && $hasTrailingVar && preg_match($regex, $this->matchHost ? $host.'.'.$trimmedPathinfo : $trimmedPathinfo, $n) && $m === (int) $n['MARK']) {
142-
$matches = $n;
143-
}
144-
145150
foreach ($vars as $i => $v) {
146151
if (isset($matches[1 + $i])) {
147152
$ret[$v] = $matches[1 + $i];

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Matcher/UrlMatcher.php
+8-4Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ 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)) {
162+
if ($hasTrailingSlash) {
163+
$matches = $m;
164+
} else {
165+
$hasTrailingVar = false;
166+
}
167+
}
168+
161169
if ('/' !== $pathinfo && !$hasTrailingVar && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
162170
if ($supportsTrailingSlash && (!$requiredMethods || \in_array('GET', $requiredMethods))) {
163171
return $this->allow = $this->allowSchemes = [];
@@ -166,10 +174,6 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
166174
continue;
167175
}
168176

169-
if ($hasTrailingSlash && $hasTrailingVar && preg_match($regex, $trimmedPathinfo, $m)) {
170-
$matches = $m;
171-
}
172-
173177
$hostMatches = [];
174178
if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
175179
continue;

‎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
@@ -187,6 +187,17 @@ public function testSlashAndVerbPrecedenceWithRedirection()
187187
$this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
188188
}
189189

190+
public function testNonGreedyTrailingRequirement()
191+
{
192+
$coll = new RouteCollection();
193+
$coll->add('a', new Route('/{a}', [], ['a' => '\d+']));
194+
195+
$matcher = $this->getUrlMatcher($coll);
196+
$matcher->expects($this->once())->method('redirect')->with('/123')->willReturn([]);
197+
198+
$this->assertEquals(['_route' => 'a', 'a' => '123'], $matcher->match('/123/'));
199+
}
200+
190201
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
191202
{
192203
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.