Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Laravel(Lumen)中的JSON响应上添加ETag头_Php_Laravel_Etag_Lumen - Fatal编程技术网

Php 如何在Laravel(Lumen)中的JSON响应上添加ETag头

Php 如何在Laravel(Lumen)中的JSON响应上添加ETag头,php,laravel,etag,lumen,Php,Laravel,Etag,Lumen,我有两条路线: $app->get('time1', function(){ return response('time1 = '.time()); }); $app->get('time2', function(){ return response()->json(['time2' => time()]); }); 和一个全局中间件: public function handle($request, Closure $next) { $resp

我有两条路线:

$app->get('time1', function(){
    return response('time1 = '.time());
});
$app->get('time2', function(){
    return response()->json(['time2' => time()]);
});
和一个全局中间件:

public function handle($request, Closure $next)
{
    $response = $next($request);
    $response->setEtag(md5($response->getContent()));
    return $response;
}
在第一种情况下,我有这个HTTP头:

ETag:"8114ac3b0aad6e54345ee00f78959316"

但不是在第二次。为什么?如何在第二种情况下添加相同的头?

在第二个响应中看到noETag的原因是,由于服务器对返回的响应进行了压缩,因此此头被web服务器剥离-请参见内容编码:gzip头。这背后的原因是,考虑到gzip具有不同的压缩级别,同一资源不能是逐字节相同的


您可以禁用gzip压缩(检查您的Apache配置,尤其是mod_deflate模块的配置),也可以不使用ETag

您确定缺少ETag吗?我已经运行了这段代码,并在两种情况下都得到了etag头set/time1日期:Sat,2015年12月12日12:48:50 GMT内容类型:text/html;charset=UTF-8内容长度:18连接:保持活动保持活动:超时=5服务器:Apache缓存控制:私有,必须重新验证,最大年龄=0 Etag:“a6e2b39351fc075ac5bd9b8c078c35e8”过期:Sat,2015年12月12日12:48:50 GMTGET/time2日期:Sat,2015年12月12日12:49:58 GMT内容类型:应用程序/json传输编码:分块连接:保持活动保持活动:超时=5变化:接受编码服务器:Apache缓存控制:私有,必须重新验证,最大年龄=0过期:Sat,2015年12月12日12:49:58 GMT内容编码:gzip此问题应通过弱ETag解决。在这个答案写好后的3,5年里,Apache是否也实施了这样的降级?还是仍然无法将etag与gzip压缩结合起来?
 ....    

  public function handle($request, Closure $next)
  {

    $response = $next($request);

    if ($request->isMethod('GET'))
    {

        $etag = md5($response->getContent());

        $requestETag = str_replace('"', '', $request->getETags());



        if ($requestETag && $requestETag[0] == $etag)
        {

            // Modifies the response so that it conforms to the rules defined for a 304 status code.
            $response->setNotModified();

        }

        $response->setETag($etag);


    }

    return $response;

}