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?
Note: Event handlers are sometimes called event listeners — they are pretty much interchangeable for our purposes, although strictly speaking, they work together. The listener listens out for the event happening, and the handler is the code that is run in response to it happening.
Laravel's events provide a simple observer pattern implementation, allowing you to subscribe and listen for various events that occur within your application. Event classes are typically stored in the app/Events directory, while their listeners are stored in app/Listeners .
An event listener in Java is designed to process some kind of event — it "listens" for an event, such as a user's mouse click or a key press, and then it responds accordingly. An event listener must be connected to an event object that defines the event.
Laravel has a great eventing system. It allows you to dispatch events and attach a set of event listeners to that specific event which are run automatically. They can run synchronously or asynchronously (by running in the queue).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With