Laravel 5.2 - 会话不保存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35365952/
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 13:14:03  来源:igfitidea点击:

Laravel 5.2 - Sessions not saving

phplaravelsessionlaravel-5.2

提问by nvaughan84

I'm using Laravel 5.2 and sessions don't seem to be saving.

我正在使用 Laravel 5.2 并且会话似乎没有保存。

I've tried both file and database (after creating the sessions table) and neither seem to work.

我已经尝试了文件和数据库(在创建会话表之后),但似乎都不起作用。

I've set the file permissions to 777 on storage/framework/sessions and no sessions file is created.

我已经在存储/框架/会话上将文件权限设置为 777,并且没有创建会话文件。

To save a session I'm using:

要保存我正在使用的会话:

session(['key' => 'value']);

and to retrieve the session I'm using:

并检索我正在使用的会话:

$value = session('key');

But I can't get it to save

但我无法保存

回答by omarjebari

Ok i had a similar problem but i was already using the 'web' middleware. It turns out that if you are calling die() or dd() before your script has finished executing then your session will not persist!!

好的,我遇到了类似的问题,但我已经在使用“网络”中间件。事实证明,如果您在脚本执行完毕之前调用 die() 或 dd() ,那么您的会话将不会持续!!

See Laravel session data not sticking across page loads

查看Laravel 会话数据不会跨页面加载

回答by festie

As already answered in here: https://stackoverflow.com/a/34454031/5781420, be sure to assign the "web" middleware to your route or your controller.

正如这里已经回答的那样:https: //stackoverflow.com/a/34454031/5781420,确保将“web”中间件分配给您的路由或控制器。

You can do this via a Route::group(...);, or just a Route::?(...)->middleware('web');or even in the controller constructor itself by $this->middleware('web');

您可以通过 aRoute::group(...);或只是 aRoute::?(...)->middleware('web');或什至在控制器构造函数本身中执行此操作$this->middleware('web');