Where can I set headers in laravel

前端 未结 7 861
别那么骄傲
别那么骄傲 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 20:58

    For future readers using Laravel 5.x, this can be handled out of the box without needing to create any custom middleware.

    Laravel has the response() helper method, which you can chain headers to very easily.

    use Response;
    // Or possibly: use Illuminate\Http\Response; depending on your aliases used.
    
    // Add a series of headers
    return response($content)
        ->header('Content-Type', 'text/xml')
        ->header('X-Header-One', 'Header Value');
    
    // Or use withHeaders to pass array of headers to be added
    return response($content)
        ->withHeaders([
            'Content-Type' => 'text/xml',
            'X-Header-One' => 'Header Value'
        ]);
    

    Read more about it in the documentation, because it can handle attaching a number of things; cookies, views, and more.

提交回复
热议问题