Events
| Dispatch events | |
| Listen with subscribers | |
| Priority | |
| Stop propagation |
Dispatch events
An event is a plain object that announces that something happened, such as an order being placed. The dispatcher passes that object to every listener registered for its class.
Inject the dispatcher interface and call dispatch with a new event instance. Keep events small data carriers without business logic so listeners stay decoupled.
namespace App\Service; use App\Event\OrderPlaced; use Symfony\Component\EventDispatcher\EventDispatcherInterface; class OrderService { public function __construct(private EventDispatcherInterface $dispatcher) { } public function place(int $orderId): void { $this->dispatcher->dispatch(new OrderPlaced($orderId)); } }
Listen with subscribers
A subscriber is one class that listens to several events in a central map. The static method getSubscribedEvents links each event class to a handler method name.
Symfony registers subscribers automatically, so no manual wiring is needed in most projects. Put related handlers together to keep notification and audit logic easy to find.
namespace App\Subscriber; use App\Event\OrderPlaced; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class OrderSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents(): array { return [ OrderPlaced::class => "onOrderPlaced", ]; } public function onOrderPlaced(OrderPlaced $event): void { echo "order received"; } }
Priority
Priority controls listener order when several handlers watch the same event. A higher number runs earlier, while zero is the default and negative numbers run later.
Use priority for validation before side effects, for example checking data before sending mail. Keep the numbers small and documented so the order stays clear to new readers.
namespace App\Subscriber; use App\Event\OrderPlaced; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class OrderSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents(): array { return [ OrderPlaced::class => [ ["onValidate", 10], ["onNotify", -10], ], ]; } public function onValidate(OrderPlaced $event): void { echo "validate"; } public function onNotify(OrderPlaced $event): void { echo "notify"; } }
Stop propagation
Stopping propagation tells the dispatcher to skip the remaining listeners for the current event. It is useful when one handler already handled the event fully or found invalid data.
Call stopPropagation on the event object and return early. Use this sparingly because skipped listeners can surprise future maintainers.
namespace App\Subscriber; use App\Event\OrderPlaced; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class OrderSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents(): array { return [ OrderPlaced::class => "onOrderPlaced", ]; } public function onOrderPlaced(OrderPlaced $event): void { echo "handled"; $event->stopPropagation(); } }
Next: Messenger
Article author: Arthur Isaev