In Laravel, an event is a way to notify the application when a certain action or occurrence takes place within the system. Events are useful for decoupling different components of an application and making them more modular.

Listeners, on the other hand, are classes that define how the application should respond to a specific event. Listeners are typically registered with the application's event dispatcher, which is responsible for triggering the appropriate listeners when an event occurs.

To use events and listeners in Laravel, you can define your events and listeners in the application's app/Events and app/Listeners directories, respectively. You can then register your listeners with the event dispatcher using the Event::listen method, or by defining them in the $listen array in the EventServiceProvider.

Step 1: Register the event and listeners in the EventServiceProvider

For our events and listeners to work, we must register them in the EventServiceProvider class that has already been set up for us at the time of installing our laravel project. So go to app/Providers/EventServiceProvider.php and click

Event and Listeners in Laravel

Creating an event and listener class

To create an event class, make use of the make:event artisan command:

php artisan make:event <event name>

This command will create a new class into the app\Events folder of your application and that is all you need to create an event class.

To create a listener class, make use of the make:listener artisan command:

php artisan make:listener <listener name>

This command, like in the Event creation, will create a new class into the app\Listeners folder of your application and that is all you need to create a listener class.

Another way of creating events and listeners, this way might even be termed easier than the ones earlier mentioned, is to register events and listeners in the EventServiceProvider class, then run:

php artisan event:generate

This command will scan through the EventServiceProvider class and generate the missing events and listeners based on registration.

You might already be wondering how to register an event and map a listener to it, yea? To do that, follow the pattern below:

How to define event in event service provider

Like I earlier said, you can map more than one listener to a particular event and it will get processed in succession, following the order in which they are mapped.

Dispatching an event

To dispatch your event and trigger the listeners, there are two methods known to me as at the time of this writing:

  • event(new EventClass());
  • EventClass::dispatch();

If your event uses the Illuminate\Foundation\Events\Dispatchable trait, you may call the static dispatch method on the event. Any arguments passed to the dispatch method will be passed to the event's constructor.

You should note that public properties declared in the event class, can be accessed in the listener class which is mapped to it.

How to define listeners in laravel

How to define listiner in laravel


Post Comment