login event handling in laravel 5

后端 未结 8 2531
温柔的废话
温柔的废话 2020-12-05 11:46

i am trying to hook to the login even in my L5 app to set last login time and IP address. i can make it work with the following:

Event::listen(\'auth.login\'         


        
8条回答
  •  没有蜡笔的小新
    2020-12-05 12:05

    EDIT: this only works in 5.0.* and 5.1.*.

    For the 5.2.* solution see JuLiAnc response below.

    after working with both proposed answers, and some more research i finally figured out how to do this the way i was trying at first.

    i ran the following artisan command

    $ php artisan handler:event AuthLoginEventHandler
    

    Then i altered the generated class removing the import of the Event class and and imported the user model. I also passed User $user and $remember to the handle method since when the auth.login event is fired, thats what is passed.

    now i opened EventServiceProvided.php and modified the $listen array as follows:

    protected $listen = [
        'auth.login' => [
            'App\Handlers\Events\AuthLoginEventHandler',
        ],
    ];
    

    i realized if this doesn't work at first, you may need to

    $ php artisan clear-compiled
    

    There we go! we can now respond to the user logging in via the auth.login event using an event handler class!

提交回复
热议问题