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

我希望这个问题不会重复。

在 Laravel 中,如何在文件外部注册事件/监听器app/Providers/EventServiceProvider?另外,是否可以即时注册它们?


叮当猫咪
浏览 138回答 2
2回答

慕桂英3389331

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

杨__羊羊

我理解你在这里的沮丧。我可以与您分享我的 EventSubscriber,您只需在事件提供程序中注册一次,然后一旦您的平台不断发展,您就可以在此类中动态注册其他事件。<?phpnamespace 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{&nbsp; &nbsp; public function onResetPassword($data){&nbsp; &nbsp; &nbsp; &nbsp; $user = $data->user;&nbsp; &nbsp; &nbsp; &nbsp; $user_password = UserPasswordReset::where('email',$user->email)->first();&nbsp; &nbsp; &nbsp; &nbsp; if(!$user_password){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $user_password = new UserPasswordReset;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $user_password->email = $user->email;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $user_password->token = Str::upper(Str::random(6));&nbsp; &nbsp; &nbsp; &nbsp; $user_password->created_at = Carbon::now();&nbsp; &nbsp; &nbsp; &nbsp; $user_password->save();&nbsp; &nbsp; &nbsp; &nbsp; DB::commit();&nbsp; &nbsp; &nbsp; &nbsp; $token = encrypt("{$user_password->email}__{$user_password->token}");&nbsp; &nbsp; &nbsp; &nbsp; $input['token'] = $token;&nbsp; &nbsp; &nbsp; &nbsp; Mail::send('emails.password', $input, function($message) use($user){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $message->to($user->email, $user->name)->subject("Password Reset Request");&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; public function registerDevice($data){&nbsp; &nbsp; &nbsp; &nbsp; $user = $data->user;&nbsp; &nbsp; &nbsp; &nbsp; if(strlen($data->device_id) > 0 AND strlen($data->device_reg_id) > 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $device = UserDevice::where('user_id',$user->id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->where('device_id',$data->device_id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->first();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!$device){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $device = new UserDevice;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $device->user_id = $user->id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $device->device_id = $data->device_id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $device->device_reg_id = $data->device_reg_id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $device->device_brand = $data->device_brand;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $device->device_brand_model = $data->device_brand_model;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $device->device_platform = $data->device_platform;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $device->device_platform_version = $data->device_platform_version;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $device->is_login = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $device->save();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param User $user&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function onLogin($data)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $user = $data->user;&nbsp; &nbsp; &nbsp; &nbsp; $last_login = UserLogin::where('user_id',$user->id)->orderBy('created_at',"DESC")->first();&nbsp; &nbsp; &nbsp; &nbsp; if(!$last_login OR ($last_login AND $last_login->created_at->diffInMinutes(Carbon::now()) >= 60)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $login = new UserLogin;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $login->user_id = $user->id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $login->ip_address = $data->ip_address;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $login->user_agent = $data->user_agent;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $login->save();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event('badge.login',(object)['user' => $user]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Register the listeners for the subscriber.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param Dispatcher $events&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function subscribe($events)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $events->listen(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'user.device',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'App\Laravel\Jobs\Subscriber\UserSubscriber@registerDevice'&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; $events->listen(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'user.login',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'App\Laravel\Jobs\Subscriber\UserSubscriber@onLogin'&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; $events->listen(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'user.reset_password',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'App\Laravel\Jobs\Subscriber\UserSubscriber@onResetPassword'&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }}然后在您的 EventServiceProvider 上有一个订阅变量,您可以在其中放置此 UserSubscriber 类/**&nbsp; &nbsp; &nbsp;* The subscriber classes to register.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @var array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $subscribe = [&nbsp; &nbsp; &nbsp; &nbsp; \App\Laravel\Jobs\Subscriber\UserSubscriber::class,&nbsp; &nbsp; ];这将为您节省大量工作,并更好地管理平台或项目中的活动
打开App,查看更多内容
随时随地看视频慕课网APP