Skip to content

Commit

Permalink
bug #23618 [Routing] allow HEAD method to be defined first (DavidBadura)
Browse files Browse the repository at this point in the history
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: 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
  • Loading branch information
fabpot committed Jul 22, 2017
2 parents adeab15 + 52e2821 commit 4e25a61
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
Expand Up @@ -307,7 +307,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
if (in_array('GET', $methods)) {
// Since we treat HEAD requests like GET requests we don't need to match it.
$methodVariable = 'canonicalMethod';
$methods = array_filter($methods, function ($method) { return 'HEAD' !== $method; });
$methods = array_values(array_filter($methods, function ($method) { return 'HEAD' !== $method; }));
}

if (1 === count($methods)) {
Expand Down
Expand Up @@ -57,6 +57,17 @@ public function match($pathinfo)
}
not_head_and_get:

// get_and_head
if ('/get_and_head' === $pathinfo) {
if ('GET' !== $canonicalMethod) {
$allow[] = 'GET';
goto not_get_and_head;
}

return array('_route' => 'get_and_head');
}
not_get_and_head:

// post_and_head
if ('/post_and_get' === $pathinfo) {
if (!in_array($requestMethod, array('POST', 'HEAD'))) {
Expand Down
Expand Up @@ -297,6 +297,15 @@ public function getRouteCollections()
array(),
'',
array(),
array('HEAD', 'GET')
));
$headMatchCasesCollection->add('get_and_head', new Route(
'/get_and_head',
array(),
array(),
array(),
'',
array(),
array('GET', 'HEAD')
));
$headMatchCasesCollection->add('post_and_head', new Route(
Expand Down

0 comments on commit 4e25a61

Please sign in to comment.