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

查看:80
本文介绍了我的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).

知道我在做什么错吗?

可能值得一提的是,我正在使用Homestead vm(完全库存)进行测试.我也尝试过使用多个浏览器.没有cookie会发送到浏览器,我想应该将会话ID作为初始get请求的一部分发送到浏览器吗?

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的中间件类\Illuminate\Session\Middleware\StartSession负责启动会话.在L5.2之前,它是在每个请求上运行的,因为它是全局中间件堆栈的一部分.现在,它是可选的,因为L5.2希望在同一应用程序中同时允许Web UI和API.

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.

如果打开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天全站免登陆