Lesson 7 — Service Providers for Event Listeners in Laravel | A Complete Guide

Abishek
Work Done Right
Published in
3 min readMay 13, 2023

--

Laravel is a powerful PHP framework that provides a wide range of features for building web applications. One of the most useful features of Laravel is its event system. Events allow you to decouple your code and make it more reusable. You can use events to notify other parts of your application when something happens, such as a user logging in or a file being uploaded.

In Laravel, event listeners are classes that listen for specific events and take action when they are triggered. To register an event listener, you need to add it to the listen property of the EventServiceProvider class. For example, the following code registers an event listener for the UserLoggedIn event:

protected $listen = [
'UserLoggedIn' => [
'App\Listeners\UserWasLoggedIn',
],
];

When the UserLoggedIn event is triggered, Laravel will call the handle() method on all of the listeners that are registered for that event.

Service providers are classes that are automatically loaded by Laravel when your application starts up. They are a great way to register event listeners and subscribers for your application. To register an event listener or subscriber in a service provider, you need to use the listen() or subscribe() methods.

--

--

Abishek
Work Done Right

I like to write about different stuff that may be of help to people. I am a husband and a software developer constantly learning new things every single day.