Reputation: 1211
in Laravel 5.6
I have an event named DocumentSend
,
And i have many Listeners Like (SendEmail
, SendNotification
, SendSMS
),
listeners are optional (depends on document type and defined by user), now the question is:
How can i call for example DocumentSend
event with just SendSMS
listener or DocumentSend
with all the listeners?
I hope you get my mean,and tell me the best practice for my issue.
Thanks in advance
Upvotes: 2
Views: 498
Reputation: 111839
Well, the simple answers is - you can't. When you fire event all registered listeners will listen to this event and all of them will be launched.
However nothing stops you to prevent running code from listener.
For example you can fire event like this:
event(new DocumentSend($document, true, false, false));
and define constructor of DocumentSend like this:
public function __construct($document, $sendEmail, $sendNotification, $sendSms)
{
$this->document = $document;
$this->sendEmail = $sendEmail;
$this->sendNotification = $sendNotification;
$this->sendSms = $sendSms;
}
and now in each listener you can just verify correct variable, so for example in SendEmail
listener in handle
you can do it like this:
public function handle(DocumentSend $event)
{
if (!$event->sendSms) {
return;
}
// here your code for sending
}
similar you can do for other listeners.
Of course this is just example - you don't have to use 4 variables. You can set some properties to $document only to mark how it should be sent.
Upvotes: 4