0

Pretty basic things going on. Using Session Flash messages to display any success/error messages.

All routes are set-up in my 'middleware' => 'web' group.

Now my problem. ->flash() does not work whilst ->put() does.

Controller:

$request->session()->flash('alert-success', 'My flash message');

Blade:

@foreach (['danger', 'warning', 'success', 'info'] as $msg)
    @if(Session::has('alert-' . $msg))
        <p class="alert alert-{{ $msg }}">{{ Session::get('alert-' . $msg) }} <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
        </p>
    @endif
@endforeach

The return redirect('routeHere')->with('alert-success', 'My flash message'); stated in the Laravel 5 docs doesn't work either.

Edit

Laravel 5.2 Session flash not working even with web middleware duplication. Using ['middlewareGroups' => 'web'] instead of ['middleware' => 'web'] does fix the problem but assuming this is not the way to go.

Community
  • 1
  • 1
Leguam
  • 1,202
  • 2
  • 14
  • 35
  • 1
    Are You sure that You want to use it in the closest next request. Because `flash` works only for the next request... and if you want to get this messsage in any request later You should use `put`. – Filip Koblański Apr 29 '16 at 08:42
  • Yes, I want that. Same problem here: http://stackoverflow.com/questions/36279871/laravel-5-2-session-flash-not-working-even-with-web-middleware?rq=1 (Using `middlewareGroups` instead of `middleware` does work but is not the way to go I assume. – Leguam Apr 29 '16 at 08:45
  • Could You dump in a view: `{{dd($request->session()->all())}}` – Filip Koblański Apr 29 '16 at 08:48
  • `DDing` a $request in view obviously doesn't work, but I get what you mean. It won't return the flashed session data – Leguam Apr 29 '16 at 08:51

2 Answers2

0

After searching around out of curiosity for this problem i noticed an interesting update for laravel version >= 5.2.27 that applies by default the web middleware group to all the routes defined in routes.php.

Now this leads me to think that by declaring the middleware ['middleware' => 'web'] will lead to the code getting called twice, thus clearing the non-persistent flash messages.

A solution based on my assumption would be to simply not use:

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

This is where i found the update information :) Laravel 5.2 Auth not Working

Community
  • 1
  • 1
Jonid Bendo
  • 860
  • 2
  • 10
  • 16
0

Try to remove "web" middleware in the laravel 5.2 from your routes.php file. This way helps me

M Arfan
  • 4,384
  • 4
  • 29
  • 46