Laravel 5 - Confusion between Event Handlers and Listeners
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.
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.