Laravel响应Cache-Control标头始终包含'no-cache' [英] Laravel response Cache-Control headers always containing 'no-cache'

查看:211
本文介绍了Laravel响应Cache-Control标头始终包含'no-cache'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,Laravel似乎在最后一刻操纵了响应标头"Cache-Control".我想使浏览器缓存成为可能.

For some reason Laravel seems to be manipulating the response headers 'Cache-Control' on the very last moment. I want to make browser caching possible.

class TestController extends Controller
{

    public function getTest()
    {
        $response = new \Illuminate\Http\Response('test', 200, array(
            'Cache-Control' => 'max-age='.(config('imagecache.lifetime')*60).', public',
            'Content-Length' => strlen('test'),
        ));

        $response->setLastModified(new \DateTime('now'));
        $response->setExpires(\Carbon\Carbon::now()->addMinutes(config('imagecache.lifetime')));

        return $response;
     }
}

即使我使用中间件"并死掉并转储响应,我仍然可以得到,这似乎对我来说是正确的.

Even when I use a 'after-middleware' and die and dump the response, I still get this, what seems to be right to me.

Response {#625 ▼
  +original: "test"
  +exception: null
  +headers: ResponseHeaderBag {#626 ▼
    #computedCacheControl: array:2 [▼
      "max-age" => "2592000"
      "public" => true
    ]
    #cookies: []
    #headerNames: array:5 [▶]
    #headers: array:5 [▼
      "cache-control" => array:1 [▼
        0 => "max-age=2592000, public"
      ]
      "content-length" => array:1 [▼
        0 => 4
      ]
      "date" => array:1 [▶]
      "last-modified" => array:1 [▼
        0 => "Sun, 16 Aug 2015 15:42:08 GMT"
      ]
      "expires" => array:1 [▶]
    ]
    #cacheControl: array:2 [▼
      "max-age" => "2592000"
      "public" => true
    ]
  }
  #content: "test"
  #version: "1.0"
  #statusCode: 200
  #statusText: "OK"
  #charset: null
}

方法$ response-> isCacheable()als返回true.但是,当我收到响应时,Firebug会显示以下内容:

The method $response->isCacheable() als returns true. But when I receive the response, Firebug shows the following:

Cache-Control   
no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection  
Keep-Alive
Content-Type    
text/html
Date    
Sun, 16 Aug 2015 15:42:08 GMT
Expires 
Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive  
timeout=5, max=98
Pragma  
no-cache
Server  
Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.5.15
Transfer-Encoding   
chunked
X-Powered-By    
PHP/5.5.15

我使用xampp,但是在同一台服务器上,当我仅加载html页(没有Laravel/PHP)时,它不会发送这些Cache-Control标头.

I use xampp, but on this same server when I just load an html-page (no Laravel/PHP), it does not send these Cache-Control headers.

当我设置最后修改的和过期的标头时,如何实现浏览器不接收Cache-Control标头"no-store,no-cache"?

How can I achieve that the browser does not receive the Cache-Control headers "no-store, no-cache" when I set the last-modified and expires headers?

谢谢!

推荐答案

我相信您的幻影缓存控制标头来自PHP.

I believe your phantom cache-control headers are coming from PHP.

http://php.net/manual/en/function. session-cache-limiter.php

当php.ini将 session.cache_limiter 设置为nocache(默认)时,PHP设置以下标头:

when php.ini has session.cache_limiter set to nocache (default), PHP sets the following headers:

Expires: Thu, 19 Nov 1981 08:52:00 GMT 
Cache-Control: no-store,no-cache, must-revalidate, post-check=0, pre-check=0 
Pragma: no-cache

几天来,我一直在为laravel上的cache-control苦苦挣扎:我发现在laravel中设置标头只是将它们附加到php.ini设置的标头上.我尝试在apache.conf中设置一些规则,以允许对通过laravel访问的.js和.css文件进行缓存,同时阻止对.php文件的请求进行缓存,但是这些规则失败了,因为apache会看到任何文件通过laravel作为.php文件提供服务(因为正在通过index.php访问该文件).

I have been struggling with cache-control in laravel on apache for a few days now: I found that setting the headers within laravel simply appended them on to the headers set by php.ini. I tried setting up some rules in apache.conf in order to allow caching of .js and .css files that were being accessed via laravel while preventing the caching of requests to .php files, but these rules failed as apache will see any file being served via laravel as a .php file (because it is being accessed via index.php).

最后,我决定在php.ini中将session.cache_limiter设置为"(从而跳过PHP对缓存头的处理),然后将以下内容添加到app:after()中的filter.php中.

In the end I settled for setting session.cache_limiter to '' in php.ini (thereby skipping PHPs handling of cache headers), and adding the following to filters.php in app:after()

     /*
     * Custom cache headers for js and css files
     */
    if ($request->is('*.js') || $request->is('*.css')){
        $response->header("pragma", "private");
        $response->header("Cache-Control", " private, max-age=86400");
    } else {
        $response->header("pragma", "no-cache");
        $response->header("Cache-Control", "no-store,no-cache, must-revalidate, post-check=0, pre-check=0");
    }

这篇关于Laravel响应Cache-Control标头始终包含'no-cache'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆