Where can I set headers in laravel

Trying Tobemyself picture Trying Tobemyself · Jul 9, 2013 · Viewed 87.2k times · Source

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 00:00:00 GMT'); for all my views, currently I'm doing this in all controllers while returning views, like

$headers=array('Cache-Control'=>'no-cache, no-store, max-age=0, must-revalidate','Pragma'=>'no-cache','Expires'=>'Fri, 01 Jan 1990 00:00:00 GMT');

Redirect::to('/',301,$headers);`

So instead of writing this for each and every route can it be done in global scope, so that headers are set for every view.

I tried setting headers by creating after filter, but didn't get it to work.

Can anyone tell me where can I set the headers for all my views?

UPDATE One of my view file meta content

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>

Now when i use Redirect::to('/',301,$headers) The header in firebug is

Cache-Control   max-age=0, must-revalidate, no-cache, no-store, private
Connection  Keep-Alive
Content-Type    text/html; charset=UTF-8
Date    Tue, 09 Jul 2013 14:52:08 GMT
Expires Fri, 01 Jan 1990 00:00:00 GMT

And when I use Redirect::to('/');

The header in firebug is

Cache-Control   no-cache
Connection  Keep-Alive
Content-Type    text/html; charset=UTF-8
Date    Tue, 09 Jul 2013 14:52:08 GMT

Answer

Amarnasan picture Amarnasan · Jul 23, 2015

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\Contracts\Routing\Middleware;

// If Laravel >= 5.2 then delete 'use' and 'implements' of deprecated Middleware interface.
class AddHeaders implements Middleware
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        $response->header('header name', 'header value');
        $response->header('another header', 'another value');

        return $response;
    }
}

Modify existing file app/Kernel.php

protected $middleware = [
.
.
.

        'App\Http\Middleware\AddHeaders',
    ];

And you're set.