Session is not persisting even inside web routes in 5.2
Seems that many developers face the issue, please see below the stackoverflow post:
http://stackoverflow.com/questions/34641229/larvel-5-2-session-values-not-persisting-even-when-web-middleware-is-used
Same here.
Tried using local LAMP and Homestead(Nginx).
Also tried all available drivers for session and with/without config:cache as suggested in #8172.
Tried fresh install from laravel installer and fresh install with composer create-project.
One interesting thing is that when using the file drive a new session file is created at each request.
All routes are inside the web middleware.
app/Http/routes.php
Route::group(['middleware' => ['web']], function () {
Route::get('/set', 'TestController@set');
Route::get('/get', 'TestController@get');
});
app/Http/Controllers/TestController.php
namespace Ses\Http\Controllers;
use Illuminate\Http\Request;
class TestController extends Controller
{
public function set(Request $request)
{
$request->session()->put('foo', 'bar');
$foo = $request->session()->get('foo');
$bar = $request->session()->get('bar', 'foo');
return view('foo', ['foo' => $foo, 'bar' => $bar]);
}
public function get(Request $request)
{
$request->session()->put('bar', 'baz');
$foo = $request->session()->get('foo');
$bar = $request->session()->get('bar', 'bar');
return view('foo', ['foo' => $foo, 'bar' => $bar]);
}
}
resources/views/foo.blade.php
<p>$foo is: {{ $foo }}</p>
<p>$bar is: {{ $bar }}</p>
resources/views/bar.blade.php
<p>$foo is: {{ $foo }}</p>
<p>$bar is: {{ $bar }}</p>
On the first request to /set values are:
$foo is: bar
$bar is: foo
Then if you make a request to /get values are:
$foo is:
$bar is: baz
Basically $foo isn't set anymore because we're already in a new session.
I'll do more testing tomorrow.
I have the same Issue on 5.2
I found out that sessions work if i register the routes and the middleware within a group
Route::group(['middleware' => ['web']], function () {
Route::get('aktuell', "SeitenController@getAktuell");
# other routes ...
});
But not if i assign the middleware in the Controllers constructor
function __construct()
{
$this->middleware('web');
}
(which is my preffered way..)
works though by saving session explicitly via
$request->session()->put("test3",time()."-anna");
$request->session()->save();
Tested out today with fresh install of 5.1.11 so far session working as expected.
Using Homestead right now.
I'll play with it to see if the problem appears again as mentioned in #8172 it looks like it's an odd bug that happened before in 4.* and 5.* versions.
I am having the same issue, any solution yet ?
Closing as this is a duplicate.
A duplicate of what?
Same issue here!
I had the same issue and was able to get it working by replacing
Route::group(['middleware' => ['web']], function () {
...
});
with
Route::group(['middlewareGroups' => ['web']], function () {
...
});
No idea why this works though when all the documentation suggests us to use ['middleware' => ['web']]
For those who arrive at this issue by now, you have likely encountered this issue #13000.
@danieliancu Thank you for referencing that issue. @GrahamCampbell Please refer to the duplicate issue when closing as duplicate.
@danieliancu this works as in the Kernel.php 'web' is part of $middlewareGroups variable.
@eness , It follows the concept and gave it right here too. thank you
This bloody hell issue is not fixed yet. People are sayings its not a bug. But its a bug
have tried using memcached ? i stored dynamic locale (user based) on the session with memcached is working fine just a few tricky, you can put \Illuminate\Session\Middleware\StartSession::class
into protected $middleware
, and change the session driver to memcached and session going to be persistence :smiley:
// App\Http\Middleware\Language
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
class Language
{
protected $request;
public function handle($request, Closure $next)
{
$language = $request->get('language');
if ($this->isSessionValid()) {
$this->forceSetLocaleSession($request);
}
if ($language) {
$this->storeSession($language);
}
return $next($request);
}
/**
* @return bool
*/
protected function isSessionValid()
{
return Session::has('language');
}
/**
* @param $request
* @throws \Exception
*/
protected function forceSetLocaleSession($request)
{
if (!$request instanceof Request) {
throw new \Exception('request variable does not valid object instanceof with Request class');
}
$request->setLocale($this->getCurrentSessionLanguage());
}
/**
* @return mixed
*/
protected function getCurrentSessionLanguage()
{
return Session::get('language');
}
/**
* @param $language
*/
protected function storeSession($language)
{
Session::put('language', $language);
}
}
/**
* Route Grouping With Middleware
*/
Route::group(['middleware' => ['web', 'language']], function () {
});
This is because of a change that was made to the Laravel 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:
// /app/Providers/RouteServiceProvider.php
$router->group(['namespace' => $this->namespace], function ($router) {
require app_path('Http/routes.php');
});
After the Laravel update:
// /app/Providers/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).
ob_start() on public/index.php
My issue turned out to be I was trying to use session in the controller's constructor (legacy code that added some permissions to menus). This was caused because laravel put the session storage stuff in the web routes group.
Most helpful comment
A duplicate of what?