Reputation: 30886
I'm a bit confused about the different between Events and Listeners.
I understood how you can create your events under Events
then register them and implement the Handlers in Handlers\Events
. So here I have events and the handling of events.
They work after I define them in Providers\EventServiceProvider.php
protected $listen = [
UserHasSignedUp::class => [
SendWelcomeEmail::class,
SendAdminEmail::class
]
];
So what are Listeners?
To me they seem exactly the same thing as Event Handlers?
Upvotes: 10
Views: 8093
Reputation: 2348
In your example UserHasSignedUp
is an Event
. SendWelcomeEmail
and SendAdminEmail
are two listeners "waiting" for the event UserHasSignedUp to be fired and they should implement the required business logic at handle
method of each one.
Super simple example:
Somewhere in UserController
Event::fire(new UserHasSignedUp($user)); //UserHasSignedUp is the event being fired
SendWelcomeEmail class
class SendWelcomeEmail //this is the listener class
{
public function handle(UserHasSignedUp $event) //this is the "handler method"
{
//send an email
}
}
As you can see, each event can have multiple listeners, but a listener can't listen to more than a single event. If you want a class listening to many events, you should take a look to Event Subscribers
Hope it helps.
Upvotes: 17
Reputation: 344
The only difference between them seems to be is, handler:event
is from Laravel 5.0's folder structure, and make:listener
is the new & current folder structure. Functionally, they are the same! - Upgrade Guide to Laravel 5.1
Commands & Handlers
The app/Commands directory has been renamed to app/Jobs. However, you are not required to move all of your commands to the new location, and you may continue using the make:command and handler:command Artisan commands to generate your classes.
Likewise, the app/Handlers directory has been renamed to app/Listeners and now only contains event listeners. However, you are not required to move or rename your existing command and event handlers, and you may continue to use the handler:event command to generate event handlers.
By providing backwards compatibility for the Laravel 5.0 folder structure, you may upgrade your applications to Laravel 5.1 and slowly upgrade your events and commands to their new locations when it is convenient for you or your team.
It's just the backward compatibility provided in Laravel 5.1. In other words, earlier, Jobs/Commands/Listeners were not self-handling, now they are.
Note that, handler:event
is not available after Laravel 5.1.
Upvotes: 1
Reputation: 21
Listeners vs. Handlers :
A listener listen
for a specific event to be fired. xxxxCreatedListener will only listen for xxxx
A handler can handle multiple events to be fired. For exemple, let's say you use performing CRUD operations, your handler could wait for the xxxxCreatedEvent, xxxxDeletedEvent, xxxxUpdatedEvent.
Upvotes: 0
Reputation: 881
There's not too much information on this out there, so this might just be speculation. I took a look at this video and saw that you can use handlers with commands. I think if you're using commands, that makes sense to have all your handlers in one spot. However if you're not, then having a App\Handlers\Events\Whatever
might not be as desirable as App\Listeners\Whatever
.
Upvotes: 0