UPDATE: My issue had nothing to do with Laravel sessions, it was my Service Worker that was causing sessions to mess up! Although I still don't understand why the service worker was fine for a different hostname with the same code, but after disabling it sessions work fine now! Will leave this here in case anyone else stumbles upon session issues and don't immediately think about any service workers they have.
Since migrating to Laravel 5.2 I've been having session persistence problem with Session::flash
and subsequent Session::keep
usage.
I know I'm supposed to use the web
middleware group, and I am. But that isn't solving my problem. Please see below:
app/Http/Kernel.php
protected $middlewareGroups = [
'web' => [
...
\Illuminate\Session\Middleware\StartSession::class,
]
];
app/Http/routes.php
Route::get('test', function(Request $request) {
$request->session()->flash('test_flash', "Some value");
return redirect('/flashed');
});
Route::get('flashed', function(Request $request) {
$request->session()->reflash();
return redirect('/flash-kept');
});
Route::get('flash-kept', function(Request $request) {
var_dump($request->session()->get('test_flash'));
});
The first time I hit /test
it redirects to /flash-kept
with value of test_flash
as NULL
- but second time I hit it, again it redirects but this time with correct value! And this is consistent in that every time I load any other page, then load /test
it doesn't keep the flash message, only second time hitting it does.
Can someone see anything wrong??
This is happening on a Debian machine running PHP 5.6.30
, very interestingly I cannot replicate this behaviour on my local machine, macOS running PHP 7.1
- but I don't think it's PHP version either as I quickly tried downgrading my local machine to use PHP 5.6 and I still couldn't re-create the issue! I've been staring at this for the last few hours, cannot see anything wrong!