Working with Events and Listeners
Events let different parts of a Laravel application react to something happening without tightly coupling the code that triggers it to the code that responds to it.
Why Decouple With Events
Without events, a controller might directly call code to send an email, log an entry, and update a cache all in one place. With events, the controller simply announces "this happened" and any number of listeners can react independently.
The Pieces Involved
| Piece | Role |
|---|---|
| Event | A simple class describing what happened |
| Listener | A class that reacts to the event |
| EventServiceProvider | Maps events to their listeners |
Registering the Mapping
protected $listen = [
OrderShipped::class => [
SendShipmentNotification::class,
],
];
Laravel can also auto-discover listeners without manual registration if event discovery is enabled.
Understanding this relationship sets up the next, more hands-on step: actually writing and firing a custom event.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.
