Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 62 ยท Getting Started

From Request to Response

Follow one HTTP request through the Symfony front controller and HttpKernel, see every kernel event, and run a mini kernel stand-in to watch the lifecycle.

Every Symfony application does the same job: take an HTTP request, run some of your code, and send back an HTTP response. The part of the framework that does it is the HttpKernel component, and its whole contract fits in one sentence: a kernel is a function from Request to Response. Everything else (routing, security, templates, the profiler toolbar) hangs off that function through events.

Understanding this lifecycle early pays off in every later chapter. When a listener does not fire, when a login redirect appears, or when a response header is missing, you can look at the lifecycle and know where to search. This lesson walks through the real flow and then lets you run a miniature kernel written in plain PHP.

The path of a request

When a visitor opens /blog/42, this is what happens:

  1. The web server hands the request to the front controller, public/index.php.
  2. The Runtime component boots your Kernel class and builds a Request object from PHP's superglobals.
  3. Kernel::handle($request) runs the HttpKernel pipeline (see below).
  4. The pipeline returns a Response; the runtime sends the headers and body to the browser.
  5. After the response is sent, the terminate event lets slower work (such as sending e-mail) run without making the visitor wait.
sequenceDiagram
    participant B as Browser
    participant F as public/index.php
    participant K as Kernel
    participant R as Router
    participant C as Controller
    B->>F: GET /blog/42
    F->>K: handle(Request)
    K->>K: event kernel.request
    K->>R: match /blog/42
    R-->>K: _controller = BlogController::show
    K->>K: event kernel.controller
    K->>C: call show(42)
    C-->>K: Response
    K->>K: event kernel.response
    K-->>B: Response
    K->>K: event kernel.terminate

The kernel announces each step as an event, so listeners can inspect or change the flow without touching your controllers.

The kernel events

HttpKernel::handle() dispatches these events, in this order. Each one is a hook for listeners (Symfony itself uses them for routing, security and the profiler):

Event When Typical use
kernel.request Before anything is chosen Routing, firewall, locale. A listener may return a Response and short-circuit everything
kernel.controller A controller was found Swap or wrap the controller
kernel.controller_arguments Arguments are about to be resolved Change the arguments
(controller runs) Your code
kernel.view The controller did not return a Response Convert a string, array or object into a response
kernel.response There is a Response Add headers, cookies, a toolbar
kernel.finish_request The sub-request ends Clean up request state
kernel.terminate After the response was sent Slow tasks
kernel.exception Anything threw an exception Turn it into an error Response

The most surprising event is kernel.view: a controller may return an array or an object, as long as some listener turns it into a response. By default, a controller must return a Response, and Symfony throws an error telling you so if it does not.

What is real and what is a stand-in

In real Symfony, the HttpKernel is written for you. You never call it manually in a normal project: the front controller does. Its core, simplified, looks like this (read-only):

PHP
// Simplified from Symfony\Component\HttpKernel\HttpKernel (real, read-only)
public function handle(Request $request, int $type = self::MAIN_REQUEST, bool $catch = true): Response
{
    try {
        return $this->handleRaw($request, $type);   // dispatches the events above
    } catch (\Throwable $exception) {
        if (!$catch) {
            throw $exception;
        }
        return $this->handleThrowable($exception, $request, $type); // kernel.exception
    }
}

The stand-in below follows the same structure with an event dispatcher, a route table and a controller. It is much simpler, but it has the same order of events and the same rule: a kernel.request listener that returns a response skips the controller.

PHP A mini HttpKernel with events (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Notice that the kernel.terminate listener runs after the response has been produced: in real PHP-FPM setups, the response is flushed to the browser first, and only then do terminate listeners run.

Short-circuiting and exceptions

Two details make the lifecycle powerful.

Short-circuiting. If a kernel.request listener sets a response, the kernel skips routing and the controller and goes straight to kernel.response. This is how a firewall can redirect an anonymous user to the login page before your controller even exists in the picture.

Exceptions. If anything throws (a missing page, a failed database query, your own bug), the kernel catches the error and dispatches kernel.exception. A listener turns the exception into a Response. Symfony's built-in handler produces a 404 page for NotFoundHttpException, a 403 page for access denied, and a 500 page for everything else; in the dev environment it shows the detailed exception page instead.

PHP Short-circuit and exception handling (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Sub-requests

The kernel can be called again from inside a request. This is a sub-request, used for example by Twig's render(controller(...)) to embed the output of another controller in a page. A sub-request runs the same events with MAIN_REQUEST replaced by SUB_REQUEST, which is why some listeners start with a check like if (!$event->isMainRequest()) { return; }.

Why this design matters

Because the kernel is just Request in, Response out, Symfony is easy to test: a functional test creates a Request, passes it to the kernel and asserts on the Response, without starting a web server. It is also easy to extend: you can add behaviour by listening to an event, which you will do in Events and Listeners. The next chapter starts with the piece that sits between kernel.request and your controller: the router, and your first page.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the contract of a Symfony kernel?

    HttpKernelInterface::handle() takes a Request and returns a Response. Everything else is built around that.
  2. 2. In which order do these kernel events fire for a normal successful request?

    Request first, then the controller is found, the response is produced, and terminate runs after it was sent.
  3. 3. What happens if a listener on kernel.request returns a Response?

    That is the short-circuit. Firewalls use it to redirect unauthenticated users.
  4. 4. Which event is dispatched when a controller returns something that is not a Response, such as an array?

    kernel.view gives a listener the chance to convert the return value into a Response. Without one, Symfony throws an error.

Summary

  • Every web request enters through public/index.php, which boots the Kernel and calls handle().
  • The HttpKernel pipeline fires events (kernel.request, kernel.controller, kernel.view, kernel.response, kernel.exception) and finally kernel.terminate after the response is sent.
  • A kernel.request listener can return a response and short-circuit the controller.
  • Exceptions are caught and converted to error responses by kernel.exception listeners.
  • Sub-requests reuse the same kernel; the main-request check keeps listeners from running twice.
  • The stand-in kernels here mirror the order of events; the real one is part of the HttpKernel component.

Next up: Your First Page.