Symfony

Events & Event Subscribers

EventDispatcher, event subscribers, listeners, kernel events, custom events, priorities

20 interview questionsยท
Mid-Level
1

What is the main role of the EventDispatcher component in Symfony?

Answer

The EventDispatcher component implements the Observer pattern and allows decoupling different parts of an application. It centralizes event management by enabling listeners to react to dispatched events without components knowing each other directly. This promotes a modular and extensible architecture where new features can be added without modifying existing code.

2

What is the main difference between an Event Listener and an Event Subscriber in Symfony?

Answer

An Event Subscriber implements EventSubscriberInterface and defines listened events via the static getSubscribedEvents() method. A Listener is a class or callable configured via services.yaml or the #[AsEventListener] attribute. Subscribers are self-descriptive and more portable, while Listeners offer more flexibility in external configuration.

3

How to declare an Event Subscriber in Symfony with the appropriate interface?

Answer

An Event Subscriber must implement EventSubscriberInterface and define the static getSubscribedEvents() method returning an associative array. Keys are event names and values can be the method name, an array with method and priority, or an array of arrays for multiple methods on the same event.

4

Which kernel event is triggered first when processing an HTTP request in Symfony?

5

How does the event priority system work in Symfony?

+17 interview questions

Master Symfony for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free