@@ -108,7 +108,12 @@ public function match(\$pathinfo)
108108 \$trimmedPathinfo = rtrim( \$pathinfo, '/');
109109 \$context = \$this->context;
110110 \$request = \$this->request;
111- \$requestMethod = \$context->getMethod();
111+ \$requestMethod = \$isLikeGetMethod = \$context->getMethod();
112+
113+ if ( \$requestMethod === 'HEAD') {
114+ \$isLikeGetMethod = 'GET';
115+ }
116+
112117
113118$ code
114119
@@ -219,11 +224,6 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
219224 $ hostMatches = false ;
220225 $ methods = $ route ->getMethods ();
221226
222- // GET and HEAD are equivalent
223- if (in_array ('GET ' , $ methods ) && !in_array ('HEAD ' , $ methods )) {
224- $ methods [] = 'HEAD ' ;
225- }
226-
227227 $ supportsTrailingSlash = $ supportsRedirections && (!$ methods || in_array ('HEAD ' , $ methods ));
228228 $ regex = $ compiledRoute ->getRegex ();
229229
@@ -265,20 +265,40 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
265265EOF ;
266266
267267 $ gotoname = 'not_ ' .preg_replace ('/[^A-Za-z0-9_]/ ' , '' , $ name );
268+
268269 if ($ methods ) {
269270 if (1 === count ($ methods )) {
270- $ code .= <<<EOF
271- if ( \$requestMethod != ' $ methods [0 ]') {
271+ if ($ methods [0 ] === 'HEAD ' ) {
272+ $ code .= <<<EOF
273+ if ( \$requestMethod != 'HEAD') {
274+ \$allow[] = 'HEAD';
275+ goto $ gotoname;
276+ }
277+
278+
279+ EOF ;
280+ } else {
281+ $ code .= <<<EOF
282+ if ( \$isLikeGetMethod != ' $ methods [0 ]') {
272283 \$allow[] = ' $ methods [0 ]';
273284 goto $ gotoname;
274285 }
275286
276287
277288EOF ;
289+ }
278290 } else {
291+ $ methodVariable = 'requestMethod ' ;
292+
293+ if (in_array ('GET ' , $ methods )) {
294+ // Since we treat HEAD requests like GET requests we don't need to match it.
295+ $ methodVariable = 'isLikeGetMethod ' ;
296+ $ methods = array_filter ($ methods , function ($ method ) { return $ method != 'HEAD ' ; });
297+ }
298+
279299 $ methods = implode ("', ' " , $ methods );
280300 $ code .= <<<EOF
281- if (!in_array( \$requestMethod , array('$ methods'))) {
301+ if (!in_array( \$$ methodVariable , array(' $ methods'))) {
282302 \$allow = array_merge( \$allow, array(' $ methods'));
283303 goto $ gotoname;
284304 }
0 commit comments