为了账号安全,请及时绑定邮箱和手机立即绑定

Laravel:在 EventServiceProvider 之外注册事件/监听器?

Laravel:在 EventServiceProvider 之外注册事件/监听器?

PHP
叮当猫咪 2023-07-08 17:32:43
我希望这个问题不会重复。在 Laravel 中,如何在文件外部注册事件/监听器app/Providers/EventServiceProvider?另外,是否可以即时注册它们?
查看完整描述

2 回答

?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

您可以在任何服务提供者的方法中注册您的事件、侦听器和订阅者boot。只是EventServiceProvider专门为事件提供了便利,但它只是获取这些数组并旋转它们并注册它们,无论如何您都会手动执行此操作。

在框架基本上运行了register服务提供者的所有方法之后,您可以随时注册事件和侦听器。


查看完整回答
反对 回复 2023-07-08
?
杨__羊羊

TA贡献1943条经验 获得超7个赞

我理解你在这里的沮丧。


我可以与您分享我的 EventSubscriber,您只需在事件提供程序中注册一次,然后一旦您的平台不断发展,您就可以在此类中动态注册其他事件。


<?php


namespace App\Laravel\Jobs\Subscriber;


use App\Laravel\Models\{User,UserLogin,UserEngagement,UserDevice,UserPasswordReset};

use App\Laravel\Events\UserLogger;

use Illuminate\Contracts\Events\Dispatcher;

use Carbon,Mail,Str,DB;

class UserSubscriber

{


    public function onResetPassword($data){


        $user = $data->user;


        $user_password = UserPasswordReset::where('email',$user->email)->first();


        if(!$user_password){

            $user_password = new UserPasswordReset;

            $user_password->email = $user->email;

        }


        $user_password->token = Str::upper(Str::random(6));

        $user_password->created_at = Carbon::now();

        $user_password->save();

        DB::commit();


        $token = encrypt("{$user_password->email}__{$user_password->token}");


        $input['token'] = $token;


        Mail::send('emails.password', $input, function($message) use($user){

            $message->to($user->email, $user->name)->subject("Password Reset Request");

        });

    }


    public function registerDevice($data){

        $user = $data->user;


        if(strlen($data->device_id) > 0 AND strlen($data->device_reg_id) > 0){

            $device = UserDevice::where('user_id',$user->id)

                                ->where('device_id',$data->device_id)

                                ->first();


            if(!$device){

                $device = new UserDevice;

                $device->user_id = $user->id;

                $device->device_id = $data->device_id;

            }

            $device->device_reg_id = $data->device_reg_id;

            $device->device_brand = $data->device_brand;

            $device->device_brand_model = $data->device_brand_model;

            $device->device_platform = $data->device_platform;

            $device->device_platform_version = $data->device_platform_version;

            $device->is_login = 1;

            $device->save();

        }

        

    }

    

    /**

     * @param User $user

     */

    public function onLogin($data)

    {

        $user = $data->user;

        $last_login = UserLogin::where('user_id',$user->id)->orderBy('created_at',"DESC")->first();

        if(!$last_login OR ($last_login AND $last_login->created_at->diffInMinutes(Carbon::now()) >= 60)){

            $login = new UserLogin;

            $login->user_id = $user->id;

            $login->ip_address = $data->ip_address;

            $login->user_agent = $data->user_agent;

            $login->save();


            event('badge.login',(object)['user' => $user]);

        }

    }

    

    /**

     * Register the listeners for the subscriber.

     *

     * @param Dispatcher $events

     */

    public function subscribe($events)

    {

        $events->listen(

            'user.device',

            'App\Laravel\Jobs\Subscriber\UserSubscriber@registerDevice'

        );


        $events->listen(

            'user.login',

            'App\Laravel\Jobs\Subscriber\UserSubscriber@onLogin'

        );


        $events->listen(

            'user.reset_password',

            'App\Laravel\Jobs\Subscriber\UserSubscriber@onResetPassword'

        );

    }

}

然后在您的 EventServiceProvider 上有一个订阅变量,您可以在其中放置此 UserSubscriber 类


/**

     * The subscriber classes to register.

     *

     * @var array

     */

    protected $subscribe = [

        \App\Laravel\Jobs\Subscriber\UserSubscriber::class,


    ];

这将为您节省大量工作,并更好地管理平台或项目中的活动


查看完整回答
反对 回复 2023-07-08
  • 2 回答
  • 0 关注
  • 100 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信