In my controller im trying to redirect back with a Flash message in laravel 5. This all works great. The problem is no matter how i set it up the flash message always re-app
For those of you not concerned with back button:
@if (Session::has('error_message'))
{{ Session::get('error_message') }}
{{ Session::forget('error_message') }}
@endif
and if that doesnt make it clear out, try:
@if (Session::has('error_message'))
{{ Session::get('error_message') }}
{{ Session::forget('error_message') }}
{{ Session::save() }}
@endif
Generally, when the user clicks the back button in the browser, the browser tries to display the contents of the previous page without reloading it. So it's likely not Laravel flashing to the session again, but the browser trying to help you out by caching the page for you.
Well it seems it was indeed the browser cache and after adding the code to stop cache it was ok and the message disappeared. It still is weird to me that i had to add this code to the controller to stop it from caching:
header('Cache-Control: no-store, private, no-cache, must-revalidate'); header('Cache-Control: pre-check=0, post-check=0, max-age=0, max-stale = 0', false);
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Expires: 0', false);
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header ('Pragma: no-cache');
Or this in the htaccess:
<IfModule mod_headers.c>
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
</IfModule>
But that seemed a bit overkill to me ;)
You can show flash messages using javascript and use SessionStorage
to stop repeating messages. Browser cache doesn't know if browser already shown a message, but we can use SessionStorage
to check it before display.
<div id="flash-container"></div>
...
var popupId = "{{ uniqid() }}";
if(sessionStorage) {
// prevent from showing if it exists in a storage (shown);
if(!sessionStorage.getItem('shown-' + popupId)) {
$('#flash-container').append("<div>{{ session('status') }}</div>");
}
sessionStorage.setItem('shown-' + popupId, '1');
}
I've just come across this problem and I think the only way to get around it is to AJAX load your flash messages into the page.
I've not tested this yet but I assume that when the user goes back and the AJAX request fires the previous flash messages will have already been cleared.
Step 1 : create one middleware using following command:
php artisan make:middleware PreventBackHistory
Step 2:
replace content of PreventBackHistory.php with following content:
namespace App\Http\Middleware;
use Closure;
class PreventBackHistory {
/* Handle an incoming request.
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
return $response->header('Cache-Control','no-cache, no-store, max-age=0, must-revalidate')
->header('Pragma','no-cache')
->header('Expires','Sun, 02 Jan 1990 00:00:00 GMT');
}
}
step 3: register middleware in kernal.php
'preventBackHistory' => \App\Http\Middleware\PreventBackHistory::class,
step 4: use middleware in your controller's construstor
public function __construct()
{
$this->middleware('preventBackHistory');
$this->middleware('auth');
}
And good to go :)