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
Create layout. blade. php file in layout folder or include header and footer which are make in same folder. @include() method is use for include common header and footer and @yield() method add content from all blade files.
getting a custom header in Laravel 5.8.request()->header('x_requested_with'); I would suggest using Accessing-From: admin which will add the apache header HTTP_ACCESSING_FROM . And you will be able to access it via the header function like this...
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.
There are a couple of different ways you could do this - all have advantages/disadvantages.
Option 1 (simple): Since the array is just static data - just manually put the headers in your view layouts directly - i.e. dont pass it from anywhere - code it straight in your view.
<?php //set headers to NOT cache a page header("Cache-Control: no-cache, must-revalidate"); //HTTP 1.1 header("Pragma: no-cache"); //HTTP 1.0 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past ?>
Option 2: Use view composers. You can use an App before filter to bind the header to all views in your app.
App::before(function($request) { $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'); View::share('headers', $headers); });
Then just echo out the $headers in your view(s).
Note: you must let the view set your headers - that is why we are 'passing' the header into view for Laravel to handle. If you try and output the header itself from within a filter or something, you'll cause issues.
Edit Option 3: I just found out about this - you could try this
App::before(function($request) { Response::header('Cache-Control', 'nocache, no-store, max-age=0, must-revalidate'); Response::header('Pragma', 'no-cache'); Response::header('Expires', 'Fri, 01 Jan 1990 00:00:00 GMT'); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With