即使使用 Web 中间件,Laravel 5.2 会话值也不会持久

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

Laravel 5.2 Session values not persisting even when web middleware is used

laravelsessionlaravel-5.2

提问by Chris Schmitz

I've got a Laravel 5.2 project that I'm using as a mock API for a javascript client I'm building out. The mock API functionality will be replaced with a different Laravel project later. For now, I just need to be able to submit API calls and get expected responses.

我有一个 Laravel 5.2 项目,我将它用作我正在构建的 javascript 客户端的模拟 API。模拟 API 功能稍后将替换为不同的 Laravel 项目。现在,我只需要能够提交 API 调用并获得预期的响应。

In my mock API I'm storing the authentication status in the session.

在我的模拟 API 中,我将身份验证状态存储在会话中。

The problem I'm running into is that the values I put into the session are not persisting between http calls.

我遇到的问题是我放入会话的值在 http 调用之间没有持久化。

session put is not persisting gif

session put 不是持久化的 gif

This seemed similar to a different stackoverflow post I found, but the thing is I'm already using the webmiddleware for my API group.

这似乎类似于我发现的不同 stackoverflow 帖子,但问题是我已经在web为我的 API 组使用中间件。

I thought it may be a permissions on my storage folder (I'm using the default filesession driver), vagrant is the owner and has write access:

我认为这可能是我的存储文件夹的权限(我使用的是默认file会话驱动程序),vagrant 是所有者并且具有写访问权限:

storage directory permissions

存储目录权限

Plus if it was a permissions issue I would think it would generate a runtime error.

另外,如果是权限问题,我认为它会产生运行时错误。

Is there something else I'm missing?

还有什么我想念的吗?

EDIT

编辑

Here's the contents of Config::get('session'):

以下是内容Config::get('session')

contents of config::get('session')

config::get('session') 的内容

And yep, the StartSessionclass is included in the webmiddleware group:

是的,StartSession该类包含在web中间件组中:

StartSession class in web middleware group

Web 中间件组中的 StartSession 类

Here's a shot of the browser session cookie vs the session file being created on the web server:

这是浏览器会话 cookie 与在 Web 服务器上创建的会话文件的截图:

browser cookie vs session file

浏览器 cookie 与会话文件

Here's the content of the request:

这是请求的内容:

request contents

请求内容

采纳答案by ???? ????

This is because of a change that was made to the Laravel that all routes by default are part of the "web" middleware, so assigning it again in your routes.php file ends up assigning it twice.

这是因为对 Laravel 进行了更改,默认情况下所有路由都是“web”中间件的一部分,因此在 routes.php 文件中再次分配它最终会分配两次。

The solution is either to remove the "web" middleware from your routes OR remove the automatic assignment from the RouteServiceProvider.

解决方案是从路由中删除“web”中间件,或者从 RouteServiceProvider 中删除自动分配。

Before the Laravel update:

Laravel 更新前:

  // /app/Providers/RouteServiceProvider.php
$router->group(['namespace' => $this->namespace], function ($router) {
    require app_path('Http/routes.php');
});

After the Laravel update:

Laravel 更新后:

// /app/Providers/RouteServiceProvider.php
$router->group([
    'namespace' => $this->namespace, 'middleware' => 'web',
], function ($router) {
    require app_path('Http/routes.php');
});

Notice how the new update automatically applies the "web" middleware to all routes. Simply remove it here if you wish to continue using Laravel 5.2 as you have before (manually assigning "web" middleware in your routes.php).

请注意新更新如何自动将“web”中间件应用于所有路由。如果你想像以前一样继续使用 Laravel 5.2,只需在此处删除它(在你的 routes.php 中手动分配“web”中间件)。

回答by howellmartinez

I had the same issue and was able to get it to work by replacing

我遇到了同样的问题,并且能够通过更换来使其正常工作

Route::group(['middleware' => ['web']], function () {
   ...
});

with

Route::group(['middlewareGroups' => ['web']], function () {
   ...
});

No idea why this works though when all the documentation suggests that we use ['middleware' => ['web']]

尽管所有文档都建议我们使用,但不知道为什么会这样 ['middleware' => ['web']]

回答by Frisbetarian

One thing that did the trick for me was to make sure that domain in config/session.php is set to null.

对我有用的一件事是确保 config/session.php 中的域设置为 null。

回答by Flex Elektro Deimling

I have the same Issue on 5.2

我有同样的问题 5.2

I found out that sessions work if i register the routes and the middleware within a group

我发现如果我在组内注册路由和中间件,会话就可以工作

Route::group(['middleware' => ['web']], function () {
    Route::get('aktuell', "SeitenController@getAktuell");
    # other routes ... 
});

But not if i assign the middleware in the Controllers constructor

但如果我在 Controllers 构造函数中分配中间件,则不会

function __construct()
{
    $this->middleware('web');
}

(which is my preffered way..)

(这是我的首选方式..)

works though by saving session explicitly via

虽然通过显式保存会话来工作

$request->session()->put("test3",time()."-anna");
$request->session()->save();

回答by Jetse

I experienced the same problem but none of the other answers helped me. For me the Illuminate\Session\Middleware\StartSessionclass was always called as it had to be, so I knew the web middleware was called.

我遇到了同样的问题,但其他答案都没有帮助我。对我来说,这个Illuminate\Session\Middleware\StartSession类总是被调用,所以我知道调用了 web 中间件。

When googeling on the problem I came accross the following threadwhere Taylor Otwell mentioned you have to use the Session::save()after setting the session variable. Using this worked for me, but I still don't know the reason why this solved the problem.

在对这个问题进行谷歌搜索时,我遇到了以下线程,其中 Taylor Otwell 提到您必须使用Session::save()after 设置会话变量。使用它对我有用,但我仍然不知道这解决了问题的原因。