← Back to Course

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

PieceRole
EventA simple class describing what happened
ListenerA class that reacts to the event
EventServiceProviderMaps 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.

Explore Course