laravel 5.4中的事件监听器

时间:2017-05-25 07:37:17

标签: php laravel laravel-5.4

我正在使用laravel 5.4来创建我的项目并且我使用auth控制器进行登录并注册我需要的是获取用户的最后登录时间并将其存储在数据库中,当我提到我想到了创建事件的想法时听众,我做到了..

login event handling in laravel 5

这是我的

  

EventServiceProvider.php

   <?php

    namespace App\Providers;

    use Illuminate\Support\Facades\Event;
    use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

    class EventServiceProvider extends ServiceProvider
    {
        /**
         * The event listener mappings for the application.
         *
         * @var array
         */
        protected $listen = [
            'App\Events\SomeEvent' => [
                'App\Listeners\EventListener',
            ],
             'Illuminate\Auth\Events\Login' => [
                'App\Listeners\AuthLoginListener',
            ],
        ];

        /**
         * Register any events for your application.
         *
         * @return void
         */
        public function boot()
        {
            parent::boot();


        }
    }
I defined listener as AuthLoginListner and my 

> AuthLoginListner.php

<?php

namespace App\Listeners;
use Carbon\Carbon;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\User;
use Illuminate\Auth\Events\Login;
class AuthLoginListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  auth.login  $event
     * @return void
     */
    public function handle(Login $event)
    {
        return "yes";
    }
}

在这里,我现在只返回一个文本我怀疑它是如何工作的以及我在哪里得到这个是消息它没有向我显示任何错误现在有一个疑问它的工作正确与否如果是的话我得到这个消息..请任何一个人帮助我,我只是对此感到困惑......

1 个答案:

答案 0 :(得分:4)

以下是我如何实现它。

我在这里创建了一个新的Listenener App/Listeners/LogSuccessfullLogin.php

namespace App\Listeners;

use Illuminate\Auth\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use \Carbon\Carbon;

class LogSuccessfulLogin
{
    /**
    * Create the event listener.
    *
    * @return void
    */
    public function __construct()
    {
        //
    }

    /**
    * Handle the event.
    *
    * @param  Login  $event
    * @return void
    */
    public function handle(Login $event)
    {
        $event->user->last_login = Carbon::now();
        $event->user->save();
    }
}

然后在EventServiceProvider中,我观察了Illuminate\Auth\Events\Login事件:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
    * The event listener mappings for the application.
    *
    * @var array
    */
    protected $listen = [
        'Illuminate\Auth\Events\Login' => [
            'App\Listeners\LogSuccessfulLogin',
        ]
    ];

    /**
    * Register any events for your application.
    *
    * @return void
    */
    public function boot()
    {
        parent::boot();

        //
    }
}

因此,每次用户登录时,都会将时间戳保存在last_login表的users字段中。

希望这有帮助!