And*_*gan 8 php naming-conventions laravel
我有一个叫做的事件UserWasRegistered
我也有一个UserWasRegistered
从那里调用的监听器,我用它来开发一个叫做的命令:
EmailRegistrationConfirmation
NotifyAdminsNewRegistration
CreateNewBillingAccount
所有这些作业都将在UserWasRegistered
事件监听器类中执行.
这是正确的方法还是我应该有多个听众UserWasRegistered
?我觉得使用工作方法使我能够在不同的时间从我的应用程序中的其他区域调用那些"工作".例如,CreateNewBillingAccount
如果用户更改了他们的详细信息,可能会调用...?
我建议更改监听器名称,以便更清楚地了解正在发生的事情,因此,避免将监听器与事件直接配对。
我们使用的是贫乏的事件/侦听器方法,因此侦听器会将实际任务传递给“执行者”(工作,服务,您为它命名)。
这个例子来自一个真实的系统:
app / Providers / EventServiceProvider.php:
OrderWasPaid::class => [
ProvideAccessToProduct::class,
StartSubscription::class,
SendOrderPaidNotification::class,
ProcessPendingShipment::class,
LogOrderPayment::class
],
Run Code Online (Sandbox Code Playgroud)
StartSubscription侦听器:
namespace App\Modules\Subscription\Listeners;
use App\Modules\Order\Contracts\OrderEventInterface;
use App\Modules\Subscription\Services\SubscriptionCreator;
class StartSubscription
{
/**
* @var SubscriptionCreator
*/
private $subscriptionCreator;
/**
* StartSubscription constructor.
*
* @param SubscriptionCreator $subscriptionCreator
*/
public function __construct(SubscriptionCreator $subscriptionCreator)
{
$this->subscriptionCreator = $subscriptionCreator;
}
/**
* Creates the subscription if the order is a subscription order.
*
* @param OrderEventInterface $event
*/
public function handle(OrderEventInterface $event)
{
$order = $event->getOrder();
if (!$order->isSubscription()) {
return;
}
$this->subscriptionCreator->createFromOrder($order);
}
}
Run Code Online (Sandbox Code Playgroud)
这样,您可以SubscriptionCreator
在应用程序的其他区域调用作业/服务(在此示例中)。
除之外,还可以将侦听器绑定到其他事件OrderWasPaid
。