Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Event Dispatcher

Event Dispatcher Laravel Package

symfony/event-dispatcher

Symfony EventDispatcher component lets application parts communicate via events. Dispatch events and register listeners or subscribers to react to them, enabling decoupled, extensible architectures with a lightweight, reusable event system.

View on GitHub
Deep Wiki
Context7

Provides tools that allow your application components to communicate with each other by dispatching events and listening to them

Frequently asked questions about Event Dispatcher
How does Symfony EventDispatcher compare to Laravel’s built-in event system?
Symfony EventDispatcher is the foundation of Laravel’s event system but offers advanced features like priority-based listeners, subscribers, and propagation control. It’s a drop-in replacement with no breaking changes for basic usage, while unlocking Symfony’s full event-driven capabilities (e.g., union types, attribute-based listeners). For Laravel, it’s a seamless upgrade path.
Can I use this package in Laravel without replacing the existing event system?
Yes. The package integrates natively with Laravel’s `Event` facade and `event()` helper. You can augment Laravel’s system with Symfony’s advanced features (e.g., subscribers, priority listeners) while keeping existing listeners intact. No configuration is required for basic compatibility.
What Laravel versions support Symfony EventDispatcher?
Symfony EventDispatcher works with Laravel 8.x, 9.x, and 10.x. It’s fully compatible with Laravel’s service container and event system, including Laravel’s event facade and helpers. No version-specific constraints exist beyond PHP 8.0+ (required by Symfony 6+).
How do I register event listeners or subscribers in Laravel?
Listeners can be registered via Laravel’s `listen()` method in `EventServiceProvider` or using Symfony’s attribute-based approach (`#[AsEventListener]`). Subscribers are registered via `subscribe()` in the provider. For type safety, Symfony’s attributes (v6.0+) are recommended but optional. Example: `#[AsEventListener(event: OrderShipped::class, method: 'handle')]`.
Is Symfony EventDispatcher safe for Laravel Queues or long-running CLI processes?
Yes, but ensure you’re using Symfony EventDispatcher v8.0.8+, which fixes a memory leak in long-running processes. For Laravel Queues or Artisan commands, the package is now stable. Avoid older versions (pre-8.0.8) where memory leaks could occur in persistent processes like cron jobs or queue workers.
Can I use this for real-time event broadcasting (e.g., WebSockets) in Laravel?
Absolutely. Pair Symfony EventDispatcher with Laravel Echo/Pusher or libraries like BeyondCode/Laravel WebSockets to broadcast events in real time. Dispatch events normally, then map them to broadcasts in listeners. Example: Dispatch `UserLoggedIn` event, then emit it via `Broadcast::channel()` or a WebSocket server.
What are the performance implications of using Symfony EventDispatcher in Laravel?
Performance overhead is minimal for most use cases, as the dispatcher is optimized for low-latency event handling. Complex workflows with many listeners may introduce slight delays, but Symfony’s design ensures efficient propagation. For high-throughput systems, consider async processing (e.g., Symfony Messenger or Laravel Queues) to offload event handling.
How do I handle event propagation (e.g., stopping further listeners) in Laravel?
Use `$event->stopPropagation()` in a listener to prevent subsequent listeners from executing. This is useful for workflows where only the first matching listener should act (e.g., authentication checks). Symfony’s `Event` class includes this method, and it integrates seamlessly with Laravel’s event system. Example: `if (!$user->isActive()) { $event->stopPropagation(); }`
Are there alternatives to Symfony EventDispatcher for Laravel?
Laravel’s native `illuminate/events` package is the primary alternative, but it lacks advanced features like subscribers, priority control, and Symfony’s attribute-based configuration. Other options include custom event buses (e.g., using Laravel Queues) or third-party packages like `spatie/laravel-event-sourcing`, but Symfony EventDispatcher remains the most feature-complete and widely adopted solution.
How do I test event listeners with Symfony EventDispatcher in Laravel?
Test listeners by dispatching events in PHPUnit and asserting their behavior. Use Laravel’s `Event::fake()` to verify listeners are called, or mock the dispatcher directly. For subscribers, test their `handle()` methods with mock events. Example: `Event::assertDispatched(OrderShipped::class);`. Symfony’s `EventDispatcherInterface` makes mocking straightforward in testing frameworks.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai