[5.6] 新功能播報:增加一個 HTTP 頭設定快取中介軟體

medz發表於2017-12-14

file

這個中介軟體可以在設定路由的時候簡單的設定來設定 HTTP 快取頭。

新增中介軟體

因為目前 Laravel 只是在 illuminate/http 中新增了這麼一箇中介軟體,但是並未新增到 laravel/laravelapp/Http/Kernel.php 中,所以暫時你需要手動新增到 protected $routeMiddleware

protected $routeMiddleware = [
    'cache' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
];

使用

自定義 etag:

Route::get('/my-route', function () {
    return view('welcome');
})->middleware('cache:max_age=180;s_maxage=60;etag=abc;immutable=true');

當然,你也可以使用 response 的 content 進行 md5 作為 etag:

Route::get('/my-route', function () {
    return view('welcome');
})->middleware('cache:max_age=180;s_maxage=60;etag;immutable');

吐槽

雖然在應用層支援了設定快取頭資訊,但是目前還沒有支援在請求進入前進行一個良好的 If-Not-Match 處理。

參考 PR: https://github.com/laravel/framework/pull/22389

Seven 的程式碼太渣,歡迎關注我的新擴充包 medz/cors 解決 PHP 專案程式設定跨域需求。

相關文章