After you navigate and press the back button, the message "Laravel 5" will flash again

In my controller, I am trying to redirect back using a Flash message in laravel 5. All this works fine. The problem is how I installed it, the flash message always appears again if I move, and then return using the browser's back button.

So, I have a list of users with a delete button next to each user. When I click the delete button, it calls my controller method to destroy. I made a small condition that redirects back with an error if I try to remove the owner. I tried the following different redirection methods, but in doing so I end up with the same problem that the message reappears after navigation and return through the browser back button.

1

return Redirect::route('users')->with('error_message', 'Some error msg'); 

2

 Session::flash('error_message', 'Some error msg'); return Redirect::to('users'); 

In my opinion, I select it like this:

 @if (Session::has('error_message')) {{ Session::get('error_message') }} @endif 

So this works well, I get a message. But as soon as, for example, I click a user in my list of users to go to the details page and click the back button of the browser, the message will begin to flash again. I don’t understand why it keeps blinking with this data, I got the impression that it just blinks once.

Even if I try to clear it immediately after the display (for example, below), it does not matter, will it always be displayed again?

 {!! Session::forget('error_message') !!} 
+5
source share
6 answers

Typically, when a user clicks the back button in a browser, the browser tries to display the contents of the previous page without reloading. Therefore, most likely, Laravel will not start blinking on the session, but the browser is trying to help you by caching the page for you.

+1
source

For those of you who are not associated with the return button:

 @if (Session::has('error_message')) {{ Session::get('error_message') }} {{ Session::forget('error_message') }} @endif 

and if that doesn't clear up, try:

 @if (Session::has('error_message')) {{ Session::get('error_message') }} {{ Session::forget('error_message') }} {{ Session::save() }} @endif 
+2
source

I just ran into this problem and I think the only way around this is to use AJAX to load your flash messages onto the page.

I have not tested this yet, but I assume that when the user returns and the AJAX request is triggered, the previous flash messages will already be cleared.

+1
source

You can show flash messages using javascript and use SessionStorage to stop repeating messages. The browser cache does not know if the browser was showing a message, but we can use SessionStorage to check it before displaying it.

 <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'); } 
+1
source

Well, it looks like it was the browser cache, and after adding the code to stop the cache it was fine, and the message disappeared. I still find it strange that I had to add this code to the controller in order 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 MYH:i:s') . ' GMT'); header ('Pragma: no-cache'); 

Or this is in 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 it seemed to me a little redundant;)

0
source

Step 1: create one middleware using the following command:

php artisan make: middleware PreventBackHistory

Step 2:

replace the contents of PreventBackHistory.php with the following contents:

namespace App \ Http \ Middleware;

use closure;

class PreventBackHistory {/ ** * Handle the 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, January 2, 1990 00:00:00 GMT "); }}

step 3: register the middleware in the kernal.php file

'preventBackHistory' => \ App \ Http \ Middleware \ PreventBackHistory :: class,

step 4: use middleware in construstor controller

public function __construct () {$ this-> middleware ('preventBackHistory'); $ This-> middleware ('Auth'); }

And good to go :)

0
source

All Articles