Question

When I do php artisan routes, the GET request of my app has a |HEAD. What is the purpose of having |HEAD?

Routes.php

+--------+----------------------------------+------------------------------+--------------------------------------+----------------+---------------+
| Domain | URI                              | Name                         | Action                               | Before Filters | After Filters |
+--------+----------------------------------+------------------------------+--------------------------------------+----------------+---------------+
|        | GET|HEAD /                       | home                         | HomeController@home                  |                |               |
|        | GET|HEAD user/{username}         | profile-user                 | ProfileController@user               |                |               |
|        | GET|HEAD account/change-password | account-change-password      | AccountController@getChangePassword  | auth           |               |
|        | GET|HEAD asset/encode-file/{id}  | encode-file                  | EncodeController@getEncode           | auth           |               |
|        | GET|HEAD asset/edit-file/{id}    | edit-file                    | AssetController@getEdit              | auth           |               |
|        | GET|HEAD asset/delete-file/{id}  | delete-file                  | AssetController@deleteDestroy        | auth           |               |
|        | GET|HEAD asset/upload-file-form  | upload-file-form             | AssetController@getUploadCreate      | auth           |               |
|        | GET|HEAD asset/library           | asset-library                | AssetController@getAssetLib          | auth           |               |
|        | GET|HEAD account/sign-out        | account-sign-out             | AccountController@getSignOut         | auth           |               |
|        | GET|HEAD account/activate/{code} | account-activate             | AccountController@getActivate        | guest          |               |
|        | GET|HEAD account/forgot-password | account-forgot-password      | AccountController@getForgotPassword  | guest          |               |
|        | GET|HEAD account/recover/{code}  | account-recover              | AccountController@getRecover         | guest          |               |
|        | GET|HEAD account/sign-in         | account-sign-in              | AccountController@getSignIn          | guest          |               |
|        | GET|HEAD account/create          | account-create               | AccountController@getCreate          | guest          |               |
+--------+----------------------------------+------------------------------+--------------------------------------+----------------+---------------+
Was it helpful?

Solution 2

Following function is taken from the Laravel's Illuminate\Routing\Router.php class, when you use Route::get() method to add a route for your site/application, Laravel adds both methods for the url, it means that, these urls registered using getmethod could be accessed using both GET and HEAD HTTP method, and HEAD is just another HTTP verb/method, used for making a HEAD request.

/**
 * Register a new GET route with the router.
 *
 * @param  string  $uri
 * @param  \Closure|array|string  $action
 * @return \Illuminate\Routing\Route
 */
public function get($uri, $action)
{
    return $this->addRoute(array('GET', 'HEAD'), $uri, $action);
}

OTHER TIPS

The HEAD request is almost identical to a GET request, they only differ by a single fundamental aspect: the HEAD response should not include a payload (the actual data).

This makes the HEAD HTTP verb fundamental for managing the validity of your current cached data.

The value for a header field in the response of your HEAD request will warn you if your data is not up-to-date. After that you can make a proper GET request retrieving the updated data.

This can be achieved observing the Content-Length field or the Last-Modified field for example.

When working with large payloads, caching your data and making HEAD requests before the actual GET to check the validity of you current data can save you big money on data consumption.

You will know precisely when to retrieve the full payload.

The big question is: why is Laravel combining HEAD and GET HTTP verbs when you use Route::get()?

You can use Route::match('HEAD') to register your HEAD request, but I find it weird that we don't have Route::head().

From the HTTP RFC:

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.

The response to a HEAD request MAY be cacheable in the sense that the information contained in the response MAY be used to update a previously cached entity from that resource. If the new field values indicate that the cached entity differs from the current entity (as would be indicated by a change in Content-Length, Content-MD5, ETag or Last-Modified), then the cache MUST treat the cache entry as stale.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top