Laravel - Routes GET|HEAD

前端 未结 2 965
南方客
南方客 2021-01-16 11:39

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

相关标签:
2条回答
  • 2021-01-16 12:00

    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);
    }
    
    0 讨论(0)
  • 2021-01-16 12:06

    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.

    0 讨论(0)
提交回复
热议问题