Laravel 5如何全局设置Cache-Control HTTP标头 [英] Laravel 5 how to set Cache-Control HTTP header globally?

查看:67
本文介绍了Laravel 5如何全局设置Cache-Control HTTP标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Laravel应用程序默认为每个站点返回Cache-Control: no-cache, private HTTP标头.我该如何改变这种行为?

My Laravel application is returning Cache-Control: no-cache, private HTTP header by default for each site. How can I change this behaviour?

P.S .:这不是PHP.ini的问题,因为将session.cache_limiter更改为空/公共不会改变任何内容.

P.S.: It is not a PHP.ini problem, because changing session.cache_limiter to empty/public does not change anything.

推荐答案

Laravel 5.5<

您可以为此使用全局中间件.像这样:

Laravel 5.5 <

You can have a global middleware for that. something like:

<?php

namespace App\Http\Middleware;

use Closure;

class CacheControl
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        $response->header('Cache-Control', 'no-cache, must-revalidate');
        // Or whatever you want it to be:
        // $response->header('Cache-Control', 'max-age=100');

        return $response;
    }
}

然后将其注册为内核文件中的全局中间件:

then just register this as a global middleware in the Kernel file:

protected $middleware = [
    ....
    \App\Http\Middleware\CacheControl::class
];

这篇关于Laravel 5如何全局设置Cache-Control HTTP标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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