symfony/event-dispatcher-contracts
Interfaces and base abstractions for Symfony’s event dispatching system. Use these contracts to standardize how events and listeners interact, and to build libraries compatible with Symfony components and their proven implementations.
Start by installing the package via Composer: composer require symfony/event-dispatcher-contracts. This package itself provides only interfaces—no concrete implementation—so it’s meant to be used alongside an actual event dispatcher (e.g., symfony/event-dispatcher, Laravel’s Illuminate\Events\Dispatcher, or PSR-14 compliant ones like league/event).
The first real use case is to define events as value objects implementing Symfony\Contracts\EventDispatcher\Event. In Laravel, you might define an event like this:
use Symfony\Contracts\EventDispatcher\Event;
class UserRegistered extends Event
{
public function __construct(public User $user) {}
}
Then dispatch it using Laravel’s event() helper or Symfony’s dispatcher—this contract ensures your event classes stay decoupled from the underlying dispatcher implementation.
Event as the base class for domain-specific events. This keeps event classes reusable across frameworks (e.g., same events in a Laravel app and a standalone CLI tool using Symfony dispatcher).getEventName() to return a string identifier—useful for debugging or custom dispatchers.public const NAME = 'user.registered') to avoid typos and support IDE autocompletion.Event class already extends Symfony’s Event, so you can start using contracts-based events immediately. To enforce the pattern, update EventServiceProvider to type-hint Symfony\Contracts\EventDispatcher\Event where appropriate.psr/event-dispatcher, your events also satisfy PSR-14 EventInterface, enabling compatibility with PSR-14 dispatchers (e.g., via symfony/event-dispatcher’s PSR adapter).symfony/event-dispatcher for Symfony apps, Laravel’s built-in dispatcher).eventName() by Default: The contract does not require an eventName() method. Laravel’s Event adds this automatically, but custom implementations must define it if needed for manual event registration.Event class is intentionally minimal (just a name property internally). Want to add metadata? Extend it—but avoid overriding core behavior to preserve interoperability.symfony/var-dumper can help inspect event objects mid-dispatch.How can I help you explore Laravel packages today?