Technical Mid Level

How do kernel events and event subscribers work in Symfony? Give examples of when you would use them instead of controller logic.

Quick Tip

Give a concrete example: "I use a kernel.response subscriber to add Cache-Control and CSP headers to every response. This keeps controllers focused on business logic and ensures no endpoint misses the security headers."

What good answers include

Symfony dispatches events at key points in the request lifecycle: kernel.request, kernel.controller, kernel.response, kernel.exception, kernel.terminate. Event subscribers listen to these events and execute cross-cutting logic. Use cases: adding security headers on every response, logging request metadata, modifying responses globally, handling exceptions with custom error pages, and running post-response cleanup. Strong candidates discuss: event priority, the difference between listeners and subscribers, and when middleware-style logic belongs in an event subscriber versus a controller.

What interviewers are looking for

Tests architectural understanding. Candidates who duplicate cross-cutting concerns across controllers do not understand the event system. Those who use kernel events for request-scoped logic and kernel.terminate for post-response work demonstrate framework maturity.

← All Symfony questions