php Laravel 用户登录时如何设置会话变量

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

How to Set Session Variable when User Login in Laravel

phplaravellaravel-5session-variableslaravel-5.3

提问by Karthik

Invoice app development is going on using Laravel. I store date and amount format for every users in settingstable.

发票应用程序正在使用 Laravel 进行开发。我将每个用户的日期和金额格式存储在settings表中。

When user login to their account how to set Sessionvariable? Please give any suggestions. I am using Laravel 5.3.

当用户登录他们的帐户时如何设置Session变量?请提出任何建议。我正在使用 Laravel 5.3。

回答by Shawn Lindstrom

Of course the docs tell us how to store session data*, but they don't address the OP's question regarding storing session data at login. You have a couple options but I think the clearest way is to override the AuthenticatesUsers trait's authenticated method.

当然,文档告诉我们如何存储会话数据*,但它们没有解决 OP 关于在登录时存储会话数据的问题。您有几个选择,但我认为最明确的方法是覆盖 AuthenticatesUsers 特征的身份验证方法。

Add the override to your LoginController:

将覆盖添加到您的 LoginController:

/**
 * The user has been authenticated.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  mixed  $user
 * @return mixed
 */
protected function authenticated(Request $request, $user)
{
    $this->setUserSession($user);
}

Then you can set your session up as:

然后您可以将会话设置为:

protected function setUserSession($user)
{
    session(
        [
            'last_invoiced_at' => $user->settings->last_invoiced_at,
            'total_amount_due' => $user->settings->total_amount_due
        ]
    );
}

If you want to be a bit more clever you can create a listener for the Login or Authenticated events and set up the session when one of those events* fires.

如果您想更聪明一点,您可以为 Login 或 Authenticated 事件创建一个侦听器,并在这些事件之一触发时设置会话。

Create a listener such as SetUpUserSession:

创建一个侦听器,例如 SetUpUserSession:

<?php

namespace app\Listeners;

use Illuminate\Auth\Events\Login;

class SetUserSession
{
    /**
     * @param  Login $event
     * @return void
     */
    public function handle(Login $event)
    {
        session(
            [
                'last_invoiced_at' => $event->user->settings->last_invoiced_at, 
                'total_amount_due' => $event->user->settings->total_amount_due
            ]
        );
    }
}

*Links go to 5.4 but this hasn't changed from 5.3.

*链接转到 5.4,但这与 5.3 没有变化。

回答by MánuelPa

I've used the Auth class to manage user data, like this:

我使用 Auth 类来管理用户数据,如下所示:

public function index(){   
  $user_id = Auth::user()->id;  
}

But you have to add 'use Auth;' before class declaration. Then you can add any data to session variable.

但是您必须添加“使用身份验证;” 在类声明之前。然后您可以将任何数据添加到会话变量。

回答by Sekonda

You can store data in the session using two different methods either a Request instance or using the global helper/function provided.

您可以使用两种不同的方法将数据存储在会话中,即请求实例或使用提供的全局帮助程序/函数。

Request Instance

请求实例

public function methodA(Request $request) {
    $request->session()->put('KEY', 'VALUE');
}

Global Helper

全球帮手

public function methodB() {
    session(['key' => 'value']);
}

You can find more details on both methods in the documentation.

您可以在文档中找到有关这两种方法的更多详细信息。