为什么我的 Laravel 5.7 会话不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54768538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:17:18  来源:igfitidea点击:

Why is my Laravel 5.7 session not working?

phplaravelsessionroutesconfig

提问by HartleySan

I'm trying to get sessions to work in Laravel 5.7, but I can't seem to get it working; I'm not sure why. I've looked at the official docs, looked at a bunch of SO posts, done Googling for the issue on other sites, and I still can't figure it out.

我试图让会话在 Laravel 5.7 中工作,但我似乎无法让它工作;我不知道为什么。我查看了官方文档,查看了一堆 SO 帖子,在其他网站上搜索了该问题,但我仍然无法弄清楚。

Basically, I've got a route like the following in web.php:

基本上,我有一条如下所示的路线web.php

Route::get('/some_path', 'SomeController@index');

Then, in the indexmethod of SomeController, I have the following:

然后,在 的index方法中SomeController,我有以下内容:

session(['test', 'Some Value']);
session(['test2', 'Some Other Value']);
session(['test3', 'Some Third Value']);
$value = session('test', 'Backup Value');
echo $value;

With that, I'm always getting Backup Valueechoed to the screen.

有了这个,我总是Backup Value在屏幕上回响。

If I go into /storage/framework/sessions, I see a session file that has the following content (which I have anonymized):

如果我进入/storage/framework/sessions,我会看到一个会话文件,其中包含以下内容(我已将其匿名化):

a:5:{s:6:"_token";s:40:"token-here";s:9:"_previous";a:1:{s:3:"url";s:26:"http://example.com/some_path";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}i:0;s:5:"test3";i:1;s:16:"Some Third Value";}

Basically, it looks like the session is kind of working, but it only seems to store test3. If I comment out the test3line in the controller, then it only stores test2. And even if I only have the testline in the controller, it still doesn't retrieve the value.

基本上,看起来会话有点工作,但它似乎只存储test3. 如果我注释掉test3控制器中的行,那么它只存储test2. 即使我test在控制器中只有一行,它仍然没有检索到值。

I also tried switching the session to a DB session with the proper session migrate file a la https://laravel.com/docs/5.7/session, but it's still not working.

我还尝试使用正确的会话迁移文件将会话切换到数据库会话,例如https://laravel.com/docs/5.7/session,但它仍然无法正常工作。

Basically, I have no clue what's up. I can say that I'm not using the native Laravel auth, so I'm not sure if that matters, but I feel like it shouldn't. Also, I tried restarting my localhost, but that didn't make a difference and running both of the following commands, neither of which seem to change anything:

基本上,我不知道发生了什么。我可以说我没有使用本地 Laravel 身份验证,所以我不确定这是否重要,但我觉得不应该。此外,我尝试重新启动我的本地主机,但这并没有产生任何区别并运行以下两个命令,这两个命令似乎都没有改变任何东西:

php artisan config:clear
php artisan route:clear

Lastly, I tried adding use Sessionat the top of the controller, but that doesn't seem to matter either.

最后,我尝试use Session在控制器顶部添加,但这似乎也无关紧要。

Basically, I don't know what to do. What could possibly be causing this? Thank you.

基本上,我不知道该怎么办。什么可能导致这种情况?谢谢你。

回答by Deepak Singh

What's happening here is that when you add this:

这里发生的事情是,当您添加此内容时:

session(['test', 'x'])

It adds testat the 0thindex of the session and xat 1st. Now when you again do a

它在会话的第 0 个索引处添加test,在1st处添加x。现在当你再次做一个

session(['test1', 'xx'])
它覆盖了 0th01st具有新值的会话的第一个索引。因此,当您打印会话数据时,您将获得最后一个值。您可以通过执行 add(session()->all())dd(session()->all())并在屏幕上看到相同的内容来检查这一点。如果您想建立键值关系并将数据存储在这样的地方,请使用如下语法:

session(['test' => 'x']);
session(['test1' => 'xx']);

回答by Kamlesh

Laravel 5.7 - store value in session with File system:

Laravel 5.7 - 在与文件系统的会话中存储值:

Set the value in session:

在会话中设置值:

session()->put('email', '[email protected]');

Get the value from session:

从会话中获取值:

$email = session()->get('email');

Delete the session of email:

删除电子邮件会话:

session()->forget('email');

It works like charm :)

它的作用就像魅力:)

回答by Andino Inyang

In this case, all you need to do is add a "web" middleware chain function to your route rule. e.g

在这种情况下,您需要做的就是在路由规则中添加一个“web”中间件链函数。例如

Route::get('/some_path', 'SomeController@index')->middleware('web');

Then clear cache:

然后清除缓存:

php artisan config:clear
php artisan route:clear