Senior LevelPHP 8+
How does the Symfony HttpKernel Event-Driven lifecycle process HTTP requests?
❓ Interview Prompt: Explain Symfony HttpKernel lifecycle events (kernel.request, kernel.controller, kernel.response, kernel.terminate), EventDispatcher, and request handling.
The Symfony HttpKernel Component is the underlying request pipeline powering Symfony (and inspiring Laravel's HTTP pipeline).
Sequential Event Pipeline:
1
'kernel.request': Fired upon receiving 'Request'. Routing and Security listeners resolve the active route and authenticate the user.
2
'kernel.controller': Resolves controller callable and validates parameter type hints.
3
'kernel.response': Modifies the output 'Response' (attaching CORS headers, cookies, cache policies).
4
'kernel.terminate': Executes after the response is sent to the client browser (used for background tasks like sending emails or flushing logs).
Code Implementation Example
php
<?php
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CorsResponseSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents(): array {
return [KernelEvents::RESPONSE => 'onKernelResponse'];
}
public function onKernelResponse(ResponseEvent $event): void {
$response = $event->getResponse();
$response->headers->set('Access-Control-Allow-Origin', '*');
}
}💡Subscribing to Symfony HttpKernel kernel.response event to attach CORS headers.
🎯 Interview Key Takeaway
Symfony HttpKernel emits sequential events (kernel.request to kernel.terminate) allowing subscribers to hook into request processing.
#Symfony#HttpKernel#Event Architecture#Senior
Back to all PHP 8+ Questions→