我的 Laravel 5.2.10 会话不会持续 [英] My Laravel 5.2.10 Sessions wont persist

查看:33
本文介绍了我的 Laravel 5.2.10 会话不会持续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个全新的 Laravel 5 安装,事实上我已经在多个版本上尝试过这个并且一直遇到同样的问题.

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

除了将会话驱动程序设置为 redis 之外,我没有更改任何默认设置.(基于文件的也有同样的问题).

I have not changed anything from the default except setting the session driver to redis. (File based also has the same issue).

我有两条路线设置如下

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');
});

如果我访问 url/set/abc,我会看到会话出现在 REDIS 中(我还看到使用基于文件时创建的文件).会话在 REDIS 中看起来很好,如下所示

If I visit the url /set/abc I see the session appear in REDIS (I also see the file created when using file based). The session looks fine in REDIS as shown below

127.0.0.1:6379> KEYS *
 1) "laravel:1a3ae6caff6346e4a173fdc1ab4c6eb0f138806b"
 2) "laravel:fed1af2fb44c6e625953237c3fa6fcbb05366a5c"
 3) "laravel:cb37286ccfe3e7caa20557aca840f50cb5a5f20d"

每次我访问该页面时,它都会重新创建一个新会话.

Every time I visit the page though, it recreates a new session.

session.php 文件的关键部分如下:

The key parts of session.php file is as follows:

'lifetime' => 120,

'expire_on_close' => false,

我还在 REDIS 中检查了会话变量的 TTL,它们确实在 120 分钟(相当于秒)时被初始化.

I have also checked in REDIS the TTL of the session variables and they do get initialised at 120 minutes (equivalent in seconds).

知道我做错了什么吗?

值得注意的是,我正在使用宅基地虚拟机(完全库存)来测试这个.我也尝试过使用多个浏览器.没有 cookie 被发送到浏览器,我认为应该将会话 ID 作为初始获取请求的一部分发送到浏览器?

It might be worth noting I am using a homestead vm (completely stock) to test this. I have also tried using multiple browsers. No cookies are ever sent to the browser, I presume a session id should be sent to the browser as part of the initial get request?

推荐答案

Laravel 的中间件类 IlluminateSessionMiddlewareStartSession 负责启动你的会话.在 L5.2 之前,它在每个请求上运行,因为它是全局中间件堆栈的一部分.现在,它是可选的,因为 L5.2 希望允许在同一应用程序中同时使用 Web UI 和 API.

Laravel's middleware class IlluminateSessionMiddlewareStartSession 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.

如果您打开 app/Http/Kernel.php,您会看到 StartSession 中间件是名为 web.您需要将所有路线都放在那里,以便您的示例工作.

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');
    });
});

您可以看到 web 中间件组还负责其他事情,例如在所有视图上提供 $errors 变量.

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:

默认情况下,routes.php 文件包含一个路由以及一个将 web 中间件组应用于它包含的所有路由的路由组.该中间件组为路由提供会话状态和 CSRF 保护.

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.

任何未放置在 web 中间件组中的路由将无法访问会话和 CSRF 保护,因此请确保将需要这些功能的任何路由放置在该组中.通常,您会将大部分路线放在此组中:

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:

来源:https://laravel.com/docs/5.2/routing

这篇关于我的 Laravel 5.2.10 会话不会持续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆