Laravel 5.2 Session Flash does not work even with web middleware

I am trying to implement flash messages using sessions, but cannot do this.

In my controller, I:

public function store(Request $request) { session()->flash('donald', 'duck'); session()->put('mickey', 'mouse'); return redirect()->action(' CustomerController@index ')->with('bugs', 'bunny'); } 

But when I check the session variables in the view, I can only see the values ​​from session()->put('mickey', 'mouse') .

Session:

 {"_token":"F6DoffOFb17B36eEJQruxvPe0ra1CbyJiaooDn3F","_previous":{"url":"http:\/\/localhost\/customers\/create"},"flash":{"old":[],"new":[]},"mickey":"mouse"} 

Many people have encountered this problem without having the proper routes inside the middleware. I did it too, but it still won't work.

In routes.php:

 Route::group(['middleware' => ['web']], function () { Route::get('/', function () { return view('welcome'); }); Route::get('/customers', ' CustomerController@index '); Route::get('/customers/create', ' CustomerController@create '); Route::post('/customers', ' CustomerController@store '); }); 

In Kernel.php:

 protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, ], 'api' => [ 'throttle:60,1', ], ]; 

Can someone tell me what I can do wrong here? Thanks!

+6
source share
6 answers

Bug fixed by replacing

 Route::group(['middleware' => ['web']], function () { ... }); 

with

 Route::group(['middlewareGroups' => ['web']], function () { ... }); 

I don’t know why this works, although when all the documentation assumes that we use ['middleware' => ['web']]

+28
source

This is more than likely due to changes made to the Laravel structure (v5.2.27) that all default routes are part of the web middleware, so assigning it again in the routes.php file ends by assigning it twice.

The solution is to remove the "network" middleware from your routes or remove the automatic assignment from RouteServiceProvider.

Before updating Laravel:

 // RouteServiceProvider.php $router->group(['namespace' => $this->namespace], function ($router) { require app_path('Http/routes.php'); }); 

After updating Laravel:

 // RouteServiceProvider.php $router->group([ 'namespace' => $this->namespace, 'middleware' => 'web', ], function ($router) { require app_path('Http/routes.php'); }); 

Notice how the new update automatically applies “network” middleware to all routes. Just delete it here if you want to continue using Laravel 5.2 as before (manually assigning "middleware" in your .php routes).

+5
source

Create session flash information using this code:

 <?php Session::flash("Donald", "Duck") // Or in your code style. $request->session()->flash("Donald", "Duck") ?> 

Check it out in your view with:

 @if(Session::has("Donald") {{Session::get("Donald")}} @endif 

You forgot to use $ request :)

+2
source

In the controller:

 use Session,Redirect; public function store(Request $request) { Session::flash('donald', 'duck'); Session::put('mickey', 'mouse'); return Redirect::to('/customers')->with('bugs', 'bunny'); } 

In the 'view', verify that the data is received or not:

 <?php print_r($bugs);die; ?> 

Good luck :)

+1
source

I am using the following:

In my controller:

 public function xyz(){ // code // This return redirect()->action(' homeController@index ')->with('success', 'Check! Everything done!'); // Or this return redirect('/index')->with('success', 'Check! Everything done!'); } 

In my opinion:

 @if(session('success')) {{ session('success') }} @endif 

Nothing else. Web middleware is assigned to each route.

+1
source

I don’t know why, but on Windows you need changes in your routes: middleware for middlewareGroups, for example:

change middleware to middlewareGroups

So, in your \ Kernel.php application, you need to first put StartSession into an array of middleware group:

put the StartSession at first on array of middleware group web

+1
source

All Articles