Laravel - 路由 GET|HEAD

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22118598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:07:12  来源:igfitidea点击:

Laravel - Routes GET|HEAD

laravellaravel-routing

提问by cch

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

当我这样做时php artisan routesGET我的应用程序的请求有一个|HEAD. 拥有的目的是|HEAD什么?

Routes.php

路由.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          |               |
+--------+----------------------------------+------------------------------+--------------------------------------+----------------+---------------+

回答by álvaro Guimar?es

The HEADrequest is almost identical to a GETrequest, they only differ by a single fundamental aspect: the HEADresponse should not include a payload (the actual data).

HEAD请求几乎是相同的一个GET请求,他们只由单个基本方面有所不同:HEAD响应不应包括有效载荷(实际数据)。

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

这使得HEAD HTTP 动词成为管理当前缓存数据有效性的基础。

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

HEAD如果您的数据不是最新的,您请求的响应中标头字段的值将警告您。之后,您可以发出适当的GET请求来检索更新的数据。

This can be achieved observing the Content-Lengthfield or the Last-Modifiedfield for example.

例如,这可以通过观察Content-Length场或Last-Modified场来实现。

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

在处理大型有效负载时,缓存您的数据并HEAD在实际之前发出请求GET以检查当前数据的有效性可以为您节省大量的数据消耗费用。

You will know precisely when to retrieve the full payload.

您将准确地知道何时检索完整的有效载荷。

The big question is: why is Laravel combining HEADand GETHTTP verbs when you use Route::get()?

最大的问题是:为什么Laravel结合HEADGETHTTP动词,当你使用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().

您可以使用Route::match('HEAD')来注册您的 HEAD 请求,但我觉得很奇怪我们没有Route::head().

From the HTTP RFC:

来自 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.

HEAD 方法与 GET 相同,除了服务器不得在响应中返回消息正文。包含在响应 HEAD 请求的 HTTP 头中的元信息应该与响应 GET 请求发送的信息相同。此方法可用于获取有关请求所隐含的实体的元信息,而无需传输实体主体本身。这种方法通常用于测试超文本链接的有效性、可访问性和最近的修改。

对 HEAD 请求的响应可以是可缓存的,因为响应中包含的信息可以用于从该资源更新先前缓存的实体。如果新字段值表明缓存实体与当前实体不同(如内容长度、内容 MD5、ETag 或 Last-Modified 的变化所表明的),则缓存必须将缓存条目视为陈旧的。

回答by The Alpha

Following function is taken from the Laravel's Illuminate\Routing\Router.phpclass, when you use Route::get()method to add a route for your site/application, Laraveladds both methods for the url, it means that, these urls registered using getmethod could be accessed using both GETand HEADHTTPmethod, and HEADis just another HTTPverb/method, used for making a HEADrequest.

以下函数取自Laravel'sIlluminate\Routing\Router.php类,当您使用Route::get()method 为您的站点/应用程序添加路由时,为Laravel加入这两个方法url,这意味着,这些urls 注册的 usingget方法可以同时使用GETHEADHTTP方法访问,而HEAD只是另一个HTTP动词/方法,用于提出HEAD请求。

/**
 * 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);
}