php – 命名Laravel事件,监听器和Jobs

前端之家收集整理的这篇文章主要介绍了php – 命名Laravel事件,监听器和Jobs前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个名为UserWasRegistered的事件,我也有一个名为UserWasRegistered的监听器,我在那里开发了一个名为的命令:

EmailRegistrationConfirmation

NotifyAdminsNewRegistration

CreateNewBillingAccount

所有这些作业都将在UserWasRegistered事件侦听器类中执行.

这是正确的方法还是我应该为UserWasRegistered提供多个监听器?我觉得使用工作方法使我能够在不同的时间从我的应用程序中的其他区域调用那些“工作”.例如如果用户更改了他们的详细信息,则可能会调用CreateNewBillingAccount …?

我建议更改更清楚地了解正在发生的事情的侦听器名称,因此我将避免直接将侦听器与事件配对.

我们正在使用一种贫血的事件/倾听者方法,因此听众将实际任务传递给“实干家”(工作,服务,您将其命名).

此示例取自实际系统:

应用程序/供应商/ EventServiceProvider.PHP

  1. OrderWasPaid::class => [
  2. ProvideAccessToProduct::class,StartSubscription::class,SendOrderPaidNotification::class,ProcessPendingShipment::class,logorderPayment::class
  3. ],

StartSubscription监听器:

  1. namespace App\Modules\Subscription\Listeners;
  2.  
  3.  
  4. use App\Modules\Order\Contracts\OrderEventInterface;
  5. use App\Modules\Subscription\Services\SubscriptionCreator;
  6.  
  7. class StartSubscription
  8. {
  9. /**
  10. * @var SubscriptionCreator
  11. */
  12. private $subscriptionCreator;
  13.  
  14. /**
  15. * StartSubscription constructor.
  16. *
  17. * @param SubscriptionCreator $subscriptionCreator
  18. */
  19. public function __construct(SubscriptionCreator $subscriptionCreator)
  20. {
  21. $this->subscriptionCreator = $subscriptionCreator;
  22. }
  23.  
  24. /**
  25. * Creates the subscription if the order is a subscription order.
  26. *
  27. * @param OrderEventInterface $event
  28. */
  29. public function handle(OrderEventInterface $event)
  30. {
  31. $order = $event->getOrder();
  32.  
  33. if (!$order->isSubscription()) {
  34. return;
  35. }
  36.  
  37. $this->subscriptionCreator->createFromOrder($order);
  38. }
  39. }

这样,您可以在应用程序的其他区域中调用作业/服务(在此示例中为SubscriptionCreator).

除了OrderWasPaid之外,还可以将侦听器绑定到其他事件.

猜你在找的Laravel相关文章