即使使用Web中间件,Laravel 5.2会话值也不会持久 [英] Laravel 5.2 Session values not persisting even when web middleware is used

查看:89
本文介绍了即使使用Web中间件,Laravel 5.2会话值也不会持久的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

我遇到的问题是我在会话中输入的值在http调用之间不持久.

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

这似乎类似于我发现的另一个stackoverflow帖子,但问题是我已经在我的API组中使用了web中间件.

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

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

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

另外,如果这是权限问题,我会认为它将生成运行时错误.

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

还有其他我想念的东西吗?

Is there something else I'm missing?

这是Config::get('session')的内容:

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

And yep, the StartSession class is included in the web middleware group:

以下是浏览器会话cookie与Web服务器上正在创建的会话文件的快照:

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

以下是请求的内容:

推荐答案

这是因为对Laravel进行了更改,默认情况下所有路由都是网络"中间件的一部分,因此请在您的路由中再次分配它.php文件最终分配了两次.

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.

解决方案是从路由中删除网络"中间件,或者从RouteServiceProvider中删除自动分配.

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

在Laravel更新之前:

Before the Laravel update:

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

Laravel更新后:

After the Laravel update:

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

请注意,新更新如何自动将网络"中间件应用于所有路由.如果您希望像以前一样继续使用Laravel 5.2,只需在此处将其删除(在route.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会话值也不会持久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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