symfony/event-dispatcher
Symfony EventDispatcher lets application parts communicate via dispatched events and listeners/subscribers. It provides a flexible event system for decoupled architecture, supporting priority-based listeners and a consistent dispatching API for PHP apps.
Event::dispatch(), listen(), subscribe()), with zero breaking changes for existing event-driven code. The #[AsEventListener] attribute (Symfony 8+) integrates with Laravel’s attribute-based listeners (introduced in Laravel 9+).queue:work) and database-backed events (e.g., Event::dispatchSync() for immediate processing).EventServiceProvider with Symfony’s EventDispatcher and migrate listeners/subcribers incrementally.high, low). Requires refactoring or a priority adapter.$event->stopPropagation()) may need wrapper methods for Laravel’s Event class.EventDispatcher must be bound to Laravel’s container (e.g., app()->bind(EventDispatcher::class, fn() => new EventDispatcher())).TraceableEventDispatcher for long-running processes).TestEventDispatcher simplifies unit testing of event flows.MakesEvents trait or EventServiceProvider.Illuminate\Events) sufficient, or do we need Symfony’s advanced features (e.g., #[AsEventListener], EventSubscriberInterface)?TraceableEventDispatcher for leaks.)#[AsEventListener].TraceableEventDispatcher provides this.)Illuminate\Events\EventServiceProvider with Symfony’s EventDispatcher (bound to Laravel’s container).EventDispatcherInterface as a drop-in replacement for Laravel’s Dispatcher facade.Event class as Symfony’s Event objects (or create adapter classes for full parity).EventServiceProvider with a custom provider binding EventDispatcher.
// app/Providers/EventServiceProvider.php
public function register()
{
$this->app->bind(
EventDispatcherInterface::class,
fn() => new EventDispatcher()
);
}
#[AsEventListener] or EventSubscriberInterface.Event::dispatch() with app(EventDispatcherInterface::class)->dispatch().int, Laravel uses string; create a priority mapper if needed).Event::listen() with Symfony’s addListener() or attribute-based registration.| Feature | Laravel Native | Symfony EventDispatcher | Notes |
|---|---|---|---|
| Event Dispatching | ✅ Event::dispatch() |
✅ dispatcher->dispatch() |
Direct replacement possible. |
| Listeners | ✅ Event::listen() |
✅ addListener() |
Priority system differs (see below). |
| Subscribers | ✅ Event::subscribe() |
✅ addSubscriber() |
Full parity. |
| Priority System | String (high, low) |
Integer (32, -8) |
Migration needed for parity. |
| Stopable Events | ❌ No | ✅ $event->stop() |
Add wrapper method for Laravel events. |
| Attributes | ✅ (Laravel 9+) | ✅ #[AsEventListener] |
Use Symfony’s for type safety. |
| Traceable Events | ❌ No | ✅ TraceableEventDispatcher |
Enable with new TraceableEventDispatcher(). |
Illuminate\Auth\Events\Registered) to use Symfony’s dispatcher.How can I help you explore Laravel packages today?