2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Laravel5, 6] Cache-Controlを設定する

Last updated at Posted at 2023-03-14

やりたいこと

LaravelデフォルトのレスポンスヘッダのCache-Controlには no-cache privateがセットされています。
これに no-storeを追加し、↓の状態にしたい。

no-cache no-store private

追加方法

Responseのheaderを変更するためのミドルウェアSetResponseHeaderを追加し、Cache-Controlをセットしました。

$ php artisan make:middleware SetResponseHeader
app/Http/Middleware/SetResponseHeader.php
<?php
namespace App\Http\Middleware;

use Closure;

class SetResponseHeader
{
    /**
     * Handle an incoming request.
     *
     * @param  Request $request
     * @param  Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        if (!$response->isRedirect()) {
            $response->header('Cache-Control', 'no-cache, no-store, private');
        }

        return $response;
    }
}

あとはKernel.phpに追加したミドルウェアを登録すればOKです。

app/Http/Kernel.php
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\SetResponseHeader::class, //追加
        ],
    ];
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?