Events

Contents
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

Related articles
Development with PHP
What is Symfony and where it runs
Install Symfony
Your first page
Routes
Controllers
Twig basics
Twig advanced
Doctrine setup
Entities
Migrations
Repositories
Relations
Forms
Validation
Services and DI
JSON controllers
Serializer
Security intro
Users
Login form
Voters
Console commands
Events
Messenger
Mailer
Cache
Sessions
CSRF
Translations
Dotenv and env
Profiler
Testing
Fixtures
Pagination
File uploads
API Platform in one lesson
DQL
Transactions
Migrations advanced
Security advanced
HTTP client
Scheduler
Webhooks
Asset Mapper
Deployment
Performance
Capstone: blog
Capstone: JSON API
Modern Symfony tour
Hero roadmap: the whole course on one page

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: