Where can I set headers in laravel

前端 未结 7 841
别那么骄傲
别那么骄傲 2020-11-29 20:08

I want to set headers as array(\'Cache-Control\'=>\'no-cache, no-store, max-age=0, must-revalidate\',\'Pragma\'=>\'no-cache\',\'Expires\'=>\'Fri, 01 Jan 1990

7条回答
  •  独厮守ぢ
    2020-11-29 21:08

    For Laravel >= 5.2 yet, following @Amarnasan answer , although I used mine for API calls

    In Laravel 5, using Middleware, creating a new file, modifying an existing file:

    New file: app/Http/Middleware/AddHeaders.php

    header('Cache-Control', 'max-age=36000, public');
            //$response->header('another header', 'another value');
    
            return $response;
        }
    }
    

    Modify existing file app/Kernel.php so that you can use with each specific route

    protected $routeMiddleware = [
    .
    .
    .
    
            'myHeader' => \App\Http\Middleware\AddHeaders::class,
        ];
    
    And you're set.
    

    Then you can use it like so for individual routes or groups

    $api->get('cars/all', 'MyController@index')->middleware(['myHeader']);;
    

提交回复
热议问题