Skip to content

Commit 4e25a61

Browse files
committed
bug symfony#23618 [Routing] allow HEAD method to be defined first (DavidBadura)
This PR was merged into the 3.3 branch. Discussion ---------- [Routing] allow HEAD method to be defined first | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Since 3.3 it's no longer possible to set the allowed methods to HEAD followed by GET. If you try this you get an `Notice: Undefined offset: 0` error. ``` index: path: '/' defaults: _controller: AppBundle:Default:index methods: [HEAD, GET] ``` It works perfectly if you change the ordering of the allowed methods: ``` index: path: '/' defaults: _controller: AppBundle:Default:index methods: [GET, HEAD] ``` The problem has been added in this commit: symfony@dd647ff#diff-3b72491a9ba1cff58442b845ae837eb3R297 After an `array_filter` the keys will not be reset. So the key `0` does not exist anymore and this check `if ('$methods[0]' !== \$$methodVariable) {` fails. A simple `array_values` ​​fix this issue. Commits ------- 52e2821 Router: allow HEAD method to be defined first
2 parents adeab15 + 52e2821 commit 4e25a61

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
307307
if (in_array('GET', $methods)) {
308308
// Since we treat HEAD requests like GET requests we don't need to match it.
309309
$methodVariable = 'canonicalMethod';
310-
$methods = array_filter($methods, function ($method) { return 'HEAD' !== $method; });
310+
$methods = array_values(array_filter($methods, function ($method) { return 'HEAD' !== $method; }));
311311
}
312312

313313
if (1 === count($methods)) {

src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ public function match($pathinfo)
5757
}
5858
not_head_and_get:
5959

60+
// get_and_head
61+
if ('/get_and_head' === $pathinfo) {
62+
if ('GET' !== $canonicalMethod) {
63+
$allow[] = 'GET';
64+
goto not_get_and_head;
65+
}
66+
67+
return array('_route' => 'get_and_head');
68+
}
69+
not_get_and_head:
70+
6071
// post_and_head
6172
if ('/post_and_get' === $pathinfo) {
6273
if (!in_array($requestMethod, array('POST', 'HEAD'))) {

src/Symfony/Component/Routing/Tests/Matcher/Dumper/PhpMatcherDumperTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,15 @@ public function getRouteCollections()
297297
array(),
298298
'',
299299
array(),
300+
array('HEAD', 'GET')
301+
));
302+
$headMatchCasesCollection->add('get_and_head', new Route(
303+
'/get_and_head',
304+
array(),
305+
array(),
306+
array(),
307+
'',
308+
array(),
300309
array('GET', 'HEAD')
301310
));
302311
$headMatchCasesCollection->add('post_and_head', new Route(

0 commit comments

Comments
 (0)