Laravel 5 flash message flashes again after navigating away and using back button

前端 未结 6 1588
无人共我
无人共我 2020-12-19 05:13

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

6条回答
  •  我在风中等你
    2020-12-19 05:55

    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 :)

提交回复
热议问题