My Laravel 5.2.10 Sessions wont persist

后端 未结 1 376
醉梦人生
醉梦人生 2020-12-02 00:13

I have a brand new installation of Laravel 5, in fact I have tried this on multiple versions and keep hitting the same issue.

I have not changed anything from the de

相关标签:
1条回答
  • 2020-12-02 01:01

    Laravel's middleware class \Illuminate\Session\Middleware\StartSession is responsible for starting your session. Before L5.2, this ran on every request because it was part of the global middleware stack. Now, it's optional because L5.2 wants to allow for both a web UI and an API within the same application.

    If you open up app/Http/Kernel.php, you'll see that the StartSession middleware is part of a middleware group called web. You need to put all your routes inside there for your example to work.

    Route::group(['middleware' => ['web']], function () {
        Route::get('/set/{value}', function($value) {
            var_dump(Session::getId());
            Session::set('test', $value);
            return view('welcome');
        });
    
        Route::get('/get', function() {
            return 'Get ' . Session::get('test');
        });
    });
    

    You can see that the web middleware group is also responsible for other things like providing the $errors variable on all views.

    You can read more about it in the docs:

    By default, the routes.php file contains a single route as well as a route group that applies the web middleware group to all routes it contains. This middleware group provides session state and CSRF protection to routes.

    Any routes not placed within the web middleware group will not have access to sessions and CSRF protection, so make sure any routes that need these features are placed within the group. Typically, you will place most of your routes within this group:

    Source: https://laravel.com/docs/5.2/routing

    0 讨论(0)
提交回复
热议问题