在 Laravel 5.2 中使用默认身份验证路由对用户进行身份验证后设置会话数据

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

Set session data after authenticate user using default auth route in laravel 5.2

phplaravelsessionauthenticationlaravel-5.2

提问by Md. Sahadat Hossain

I am developing a project using laravel 5.2. I use default authentication in this project by running this command.

我正在使用 laravel 5.2 开发一个项目。我通过运行此命令在此项目中使用默认身份验证。

php artisan make:auth

Everything is working great. Now I want to put some data in session after authenticate the user. I can not find the place where I put my code to store data in session after authenticate the user. I googled but not found any solution.

一切都很好。现在我想在对用户进行身份验证后将一些数据放入会话中。在对用户进行身份验证后,我找不到将代码存储在会话中的位置。我用谷歌搜索但没有找到任何解决方案。

采纳答案by Md. Sahadat Hossain

Finally I got answer from another question. Here i post the solution for who are looking this type of solution

最后我从另一个问题得到了答案。在这里,我为正在寻找此类解决方案的人发布了解决方案

https://stackoverflow.com/a/36491235/2738927

https://stackoverflow.com/a/36491235/2738927

回答by Arun Code

You can check whether the user is logged in or not using the following code.

您可以使用以下代码检查用户是否已登录。

if (Auth::check()) {
    // The user is logged in...
}

Then you can use the session helper class to set the required value in session.

然后您可以使用会话助手类在会话中设置所需的值。

session()->set('session_key', 'session_value');

Additionally, you can remove the session using the forgetmethod.

此外,您可以使用忘记方法删除会话。

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

Resources to read.

阅读资源。

https://laravel.com/docs/5.3/authentication

https://laravel.com/docs/5.3/authentication

https://laravel.com/docs/5.3/session

https://laravel.com/docs/5.3/session

https://laravel.com/docs/5.3/helpers#method-session

https://laravel.com/docs/5.3/helpers#method-session

回答by Alexey Mezenin

In 5.2 you need to override login()method from Illuminate\Foundation\Auth\AuthenticatesUserstrait in AuthController.php.

在 5.2 中,您需要覆盖trait 中的login()方法。Illuminate\Foundation\Auth\AuthenticatesUsersAuthController.php

To set data use session()helper in login()method:

session()login()方法中设置数据使用助手:

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