Laravel 5.2 登录会话不持久

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

Laravel 5.2 login session not persisting

phplaravellaravel-5laravel-5.2cartalyst-sentinel

提问by Nauphal

I have been implementing a simple authentication system on Laravel 5.2 using Sentinel.

我一直在使用Sentinel在 Laravel 5.2 上实现一个简单的身份验证系统。

// Route : /login
$success = Sentinel::authenticate(array(
   'email'    => $email,
   'password' => $password,
));

echo $success ? 'Login success' : 'Login failed';

So, the above code outputs Login successafter the authentication code. But, the login status is not getting persisted to other requests. ie: if I check the authentication status from other requests, it is saying that I am not logged in!

所以,上面的代码Login success在认证码之后输出。但是,登录状态不会持久化到其他请求。即:如果我从其他请求中检查身份验证状态,则表示我没有登录!

// Route : test-login
echo \Sentinel::check() ? 'User is logged in' : 'User is not logged in';

I have even tried to implement a defaut laravel authencation using \Auth::attempt. But, that also giving the same thing.

我什至尝试使用\Auth::attempt. 但是,这也给出了同样的东西。

Any help on this is greatly appreciated.

非常感谢您对此的任何帮助。

回答by Marcin Nabia?ek

In Laravel 5.2 you need to apply webgroup middlewere to all your routes you wan't to make sessions work. This is the major change from Laravel 5.1.

在 Laravel 5.2 中,您需要将webgroup middlewere 应用于您不想让会话工作的所有路由。这是 Laravel 5.1 的主要变化。

Please look at https://laravel.com/docs/5.2/routing#basic-routing

请看https://laravel.com/docs/5.2/routing#basic-routing

The default routes.php file looks like this at the moment:

目前默认的 routes.php 文件如下所示:

Route::group(['middleware' => ['web']], function () {
    // here you should put your routes
});

EDIT

编辑

You can look also directly at https://github.com/laravel/laravel/blob/master/app/Http/Kernel.phpinto middlewareGroupsproperty to know which middlewares are fired for webgroup middleware

您也可以直接查看https://github.com/laravel/laravel/blob/master/app/Http/Kernel.phpinto middlewareGroupsproperty 以了解为web组中间件触发了哪些中间件