Skip to content

Commit 6494602

Browse files
Merge branch '4.1' into 4.2
* 4.1: [Workflow] Fixed BC break for Workflow metadata [Routing] ignore trailing slash for non-GET requests
2 parents 428edae + cda9b42 commit 6494602

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

Matcher/Dumper/PhpMatcherTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
9090

9191
if ('/' === $pathinfo || $hasTrailingSlash === ('/' === $pathinfo[-1])) {
9292
// no-op
93-
} elseif ($this instanceof RedirectableUrlMatcherInterface) {
93+
} elseif ($this instanceof RedirectableUrlMatcherInterface && (!$requiredMethods || isset($requiredMethods['GET'])) && 'GET' === $canonicalMethod) {
9494
return $allow = $allowSchemes = array();
9595
} else {
9696
continue;
@@ -139,7 +139,7 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
139139
}
140140
}
141141
if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
142-
if ($this instanceof RedirectableUrlMatcherInterface && (!$requiredMethods || isset($requiredMethods['GET']))) {
142+
if ($this instanceof RedirectableUrlMatcherInterface && (!$requiredMethods || isset($requiredMethods['GET'])) && 'GET' === $canonicalMethod) {
143143
return $allow = $allowSchemes = array();
144144
}
145145
continue;

Matcher/UrlMatcher.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ public function addExpressionLanguageProvider(ExpressionFunctionProviderInterfac
130130
*/
131131
protected function matchCollection($pathinfo, RouteCollection $routes)
132132
{
133+
// HEAD and GET are equivalent as per RFC
134+
if ('HEAD' === $method = $this->context->getMethod()) {
135+
$method = 'GET';
136+
}
133137
$supportsTrailingSlash = '/' !== $pathinfo && '' !== $pathinfo && $this instanceof RedirectableUrlMatcherInterface;
134138

135139
foreach ($routes as $name => $route) {
@@ -140,7 +144,7 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
140144
// check the static prefix of the URL first. Only use the more expensive preg_match when it matches
141145
if ('' === $staticPrefix || 0 === strpos($pathinfo, $staticPrefix)) {
142146
// no-op
143-
} elseif (!$supportsTrailingSlash || ($requiredMethods && !\in_array('GET', $requiredMethods))) {
147+
} elseif (!$supportsTrailingSlash || ($requiredMethods && !\in_array('GET', $requiredMethods)) || 'GET' !== $method) {
144148
continue;
145149
} elseif ('/' === $staticPrefix[-1] && substr($staticPrefix, 0, -1) === $pathinfo) {
146150
return $this->allow = $this->allowSchemes = array();
@@ -170,7 +174,7 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
170174
}
171175
}
172176
if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
173-
if (!$requiredMethods || \in_array('GET', $requiredMethods)) {
177+
if ((!$requiredMethods || \in_array('GET', $requiredMethods)) && 'GET' === $method) {
174178
return $this->allow = $this->allowSchemes = array();
175179
}
176180
continue;
@@ -190,11 +194,6 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
190194

191195
$hasRequiredScheme = !$route->getSchemes() || $route->hasScheme($this->context->getScheme());
192196
if ($requiredMethods) {
193-
// HEAD and GET are equivalent as per RFC
194-
if ('HEAD' === $method = $this->context->getMethod()) {
195-
$method = 'GET';
196-
}
197-
198197
if (!\in_array($method, $requiredMethods)) {
199198
if ($hasRequiredScheme) {
200199
$this->allow = array_merge($this->allow, $requiredMethods);

Tests/Matcher/UrlMatcherTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,17 @@ public function testSlashAndVerbPrecedence()
723723
'customerId' => '123',
724724
);
725725
$this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
726+
727+
$coll = new RouteCollection();
728+
$coll->add('a', new Route('/api/customers/{customerId}/contactpersons/', array(), array(), array(), '', array(), array('get')));
729+
$coll->add('b', new Route('/api/customers/{customerId}/contactpersons', array(), array(), array(), '', array(), array('post')));
730+
731+
$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST'));
732+
$expected = array(
733+
'_route' => 'b',
734+
'customerId' => '123',
735+
);
736+
$this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
726737
}
727738

728739
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)

0 commit comments

Comments
 (0)