当用户登录 Laravel 时触发事件

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

Fire an event when the user logins in Laravel

phplaravellaravel-5laravel-5.1

提问by nirvair

I am trying to implement events, where I have to fire an event when the user logs in.

我正在尝试实现事件,当用户登录时我必须在其中触发一个事件。

I am following this link - http://laravel.com/docs/5.1/events#event-subscribers, but there is no result when I log in or log out.

我正在关注此链接 - http://laravel.com/docs/5.1/events#event-subscribers,但是当我登录或注销时没有结果。

public function onUserLogin($event) {
        $user = new User;

        $user->last_login_on = Carbon::now();

        $user->save();
    }

    /**
     * Handle user logout events.
     */
    public function onUserLogout($event) {
        $user = new User;

        $user->last_login_on = Carbon::now();

        $user->save();
    }

And this is the subscribe.

这是subscribe.

public function subscribe($events)
    {
        $events->listen(
            'App\Events\UserLoggedIn',
            'App\Listeners\UserEventListener@onUserLogin'
        );

        $events->listen(
            'App\Events\UserLoggedOut',
            'App\Listeners\UserEventListener@onUserLogout'
        );
    }

Though, I am not aware of what should I put in here, App\Events\UserLoggedIn

虽然,我不知道我应该在这里放什么, App\Events\UserLoggedIn

Where can I fire this event?

我可以在哪里触发此事件?

And how do I implement this functionality using Events?

以及如何使用事件实现此功能?

回答by jedrzej.kurylo

The events you need to listen on are:

您需要监听的事件是:

  • auth.login- fired when user logged in successfully
  • auth.logout- fired when user logged out
  • auth.attempt- fired when user attempts to log in
  • auth.login- 当用户成功登录时触发
  • auth.logout- 用户登出时触发
  • auth.attempt- 当用户尝试登录时触发

Those are the events that Laravel fires automatically - see Illuminate\Auth\Guardclass which is what you get when you use Authfacade.

这些是 Laravel 自动触发的事件 - 请参阅Illuminate\Auth\Guard类,这是您在使用Auth外观时得到的 。

In order for your listeners to work you need to do:

为了让您的听众工作,您需要执行以下操作:

$events->listen(
    'auth.login',
    'App\Listeners\UserEventListener@onUserLogin'
);

$events->listen(
    'auth.logout',
    'App\Listeners\UserEventListener@onUserLogout'
);

Another option is to define separate handler classes for both events, e.g.:

另一种选择是为两个事件定义单独的处理程序类,例如:

class UserEventLoginHandler {
  public function handle($event) {
    //do some logic here
  }
}

class UserEventLogoutHandler {
  public function handle($event) {
    //do some logic here
  }
}

and then define listeners in :

然后定义监听器:

protected $listen = array(
  'auth.login' => 'App\Listeners\UserEventLoginListener',
  'auth.logout' => 'App\Listeners\UserEventLogoutListener'
);

You can also use Laravel's command to generate the handlers - see https://mattstauffer.co/blog/laravel-5.0-events-and-handlersfor more details.

您还可以使用 Laravel 的命令来生成处理程序 -有关更多详细信息,请参阅https://mattstauffer.co/blog/laravel-5.0-events-and-handlers

回答by Mushangi Derrick

I know this question is quite old but to answer the last comment in case someone else is struggling with the same issue. You also need to access the currently logged in user in your onUserLogin and onUserLogout methods since creating a new instance of the user model creates a new user rather than updating the currently logged in user.

我知道这个问题已经很老了,但要回答最后一条评论,以防其他人遇到同样的问题。您还需要在 onUserLogin 和 onUserLogout 方法中访问当前登录的用户,因为创建用户模型的新实例会创建一个新用户,而不是更新当前登录的用户。