Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 - Confusion between Event Handlers and Listeners

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?

like image 316
user391986 Avatar asked May 19 '15 21:05

user391986


People also ask

What is the difference between an event handler and an event listener?

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.

What is Laravel events and listeners?

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 .

What does a listener do in event handling?

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.

Are Laravel events synchronous?

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).


2 Answers

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.

like image 52
Ezequiel Moreno Avatar answered Nov 17 '22 22:11

Ezequiel Moreno


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.

like image 44
Nikhil Bhatia Avatar answered Nov 17 '22 23:11

Nikhil Bhatia