Laravel移除Cache-Control
碰到一个问题,网站上线后,需要移除Cache-Control,就是下面这个东西
方案1 失败
参考网址:https://stackoverflow.com/questions/51821563/laravel-5-how-to-set-cache-control-http-header-globally
修改 RouteServiceProvider.php
protected function mapWebRoutes()
{
Route::middleware('web')
->middleware('cache.headers:private;max_age=3600') // added this line
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
方案2 失败
修改.htaccess文件。添加如下内容
注意:需要apache开启mod_headers,否则可能出现500错误
<IfModule mod_headers.c>
Header unset Cache-Control
</IfModule>
方案3 成功
新建中间件
php artisan make:middleware CheckResponse
修改handle方法如下
public function handle($request, Closure $next) {
$response = $next($request);
$response->headers->remove('Cache-Control');
return $response;
}
注册中间件CheckResponse。修改kernel.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\CheckResponse::class,
],