Where can I set headers in laravel

前端 未结 7 812
别那么骄傲
别那么骄傲 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

    <?php 
    namespace App\Http\Middleware;
    use Closure;
    use Illuminate\Routing\Redirector;
    use Illuminate\Http\Request;
    use Illuminate\Foundation\Applicaion;
    
    
    class AddHeaders 
    {
        public function handle($request, Closure $next)
        {
            $response = $next($request);
            $response->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']);;
    
    0 讨论(0)
提交回复
热议问题