6

I am trying to implement flash messaging using sessions but am unable to do so.

In my controller I have:

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"}

A lot of people encountered this problem by not having the relevant routes inside the web middleware. I made sure to do this as well but it still wouldn'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 anyone let me know what I could be doing wrong here? Thanks!

akmozo
  • 9,829
  • 3
  • 28
  • 44
howellmartinez
  • 1,195
  • 1
  • 13
  • 24

6 Answers6

29

Fixed the issue by replacing

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

with

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

No idea why this works though when all the documentation suggests that we use ['middleware' => ['web']]

howellmartinez
  • 1,195
  • 1
  • 13
  • 24
  • 1
    In your case, ['middleware' => 'web'] will work too. But 'web' middleware doesn't work when we are giving array of middlewares. Though it's not in any documentation. – Sovon Apr 06 '16 at 04:44
  • I'm pretty sure I tried ['middleware' => 'web'] as well but it didn't work – howellmartinez Apr 06 '16 at 18:12
  • 1
    Thaks for saving my time. I was having the same issue like yours. Looks like the documentation should be there to tell about this. – Nirmalz Thapaz Apr 07 '16 at 05:41
  • Had ['middleware' => 'web'] and it wasn't working, but with middlewareGroups worked as expected – Agu Dondo Aug 13 '16 at 20:19
5

This is more than likely because of a change that was made to the Laravel framework (v5.2.27) that all routes by default are part of the "web" middleware, so assigning it again in your routes.php file ends up assigning it twice.

The solution is either to remove the "web" middleware from your routes OR remove the automatic assignment from the RouteServiceProvider.

Before the Laravel update:

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

After the Laravel update:

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

Notice how the new update automatically applies the "web" middleware to all routes. Simply remove it here if you wish to continue using Laravel 5.2 as you have before (manually assigning "web" middleware in your routes.php).

ChimeraTheory
  • 493
  • 1
  • 4
  • 11
1

Build your Session flash info by using this code:

<?php

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

Check it in your view with:

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

You forget to use $request :)

Chilion
  • 4,380
  • 4
  • 33
  • 48
  • Thanks for the reply, I tested this earlier and using $request didn't make a difference :( As far as I know, using `session()` without the `$request` is valid -- the one without `$request` invokes the `session()` helper (laravel.com/docs/5.2/helpers#method-session). Also, it doesn't explain why "bugs":"bunny" doesn't show up. – howellmartinez Mar 29 '16 at 10:26
1

In 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 'view' check the data is getting or not:

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

Good Luck :)

Sunil
  • 1,106
  • 1
  • 7
  • 17
  • He said he is not getting the 'bugs','bunny'. for that he should redirect to the proper route like this return Redirect::to('/customers')->with('bugs', 'bunny'); And your answer is also correct. @Chilion – Sunil Mar 29 '16 at 11:05
  • Sorry tested it multiple times and it still doesn't work. Although there was something strange where the 'donald' key appeared in the session but without the 'duck' value – howellmartinez Mar 29 '16 at 11:24
  • @howellmartinez try this : Session::keep(['donald', 'duck']); or Session::reflash(); – Sunil Mar 29 '16 at 11:29
  • `session()->keep(['donald'])` will result in session having `"flash":{"new":["donald"],"old":[]}` but still no "donald":"duck" key-value pair – howellmartinez Mar 29 '16 at 11:38
1

I use 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 view:

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

Nothing else. The web-middleware is assigned to every route.

Brotzka
  • 2,959
  • 4
  • 35
  • 56
  • Yes this code should work although it is very unusual that I can't get it to. I have another Laravel 5.2 project with a similar routes and controllers setup and sessions work fine. For this project, I even tried to do a brand new install but I still have the same problem. – howellmartinez Mar 29 '16 at 16:08
1

I dont know why but on Windows you need changes in your routes: middleware to middlewareGroups, like that:

change middleware to middlewareGroups

So, in your app\Kernel.php, you need put the StartSession at first on array of middleware group web:

put the StartSession at first on array of middleware group web

Clay
  • 4,700
  • 3
  • 33
  • 49
rafwell
  • 429
  • 1
  • 4
  • 12