php - Laravel 5.4 中的事件监听器

标签 php laravel laravel-5.4

我正在使用 laravel 5.4 创建我的项目,我使用身份验证 Controller 进行登录和注册,我需要的是获取用户的上次登录时间并将其存储在数据库中,当我提到我想到创建事件的想法时听众和我做到了..

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";
    }
}

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

最佳答案

这是我实现这一目标的方法。

我在这里创建了一个新的监听器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();

        //
    }
}

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

希望这有帮助!

关于php - Laravel 5.4 中的事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44174931/

相关文章:

php - Mysql查询语法问题

PHP MySQL 多个搜索单词使用数组进行单词到单词的翻译

Laravel 更新请求验证如何获取id

laravel - 无法让 laravel 在 Azure Web App Linux 上运行

phpunit - Laravel 使用 phpunit + 多进程测试登录凭据

php - php中的多个条件和赋值

laravel - 缺少[Route : roles.destroy]的必填参数

php - 如何将默认表名 `users` 更改为自定义名称

mysql - Laravel 关系,我需要额外的表格来关联评论吗

javascript - 如何根据客户位置更改时区