Back to Blog
Architecture

Request Lifecycle for a Mezzio-Based Application

Seamlessly Interconnected Middleware for Enterprise-Level Solutions

The request lifecycle is the sequence of steps that happen from the moment a user makes an HTTP request until the server sends back a response.

The graph below shows how the request is handled by Dotkernel Light (GitHub, documentation), one of the applications in the Dotkernel Headless Platform suite.

Hover over items for description

Entry Point

1. HTTP Request
[public/index.php]
2. Service Container
3. Route Registration
4. Middleware Pipeline
[config/pipeline.php]

5. Routing

6. Handler
Invocation

7. Custom Logic
Execution in Handler

8. Template
Rendering [twig]

9. Response
Creation

10. Response
Pipeline

11. Response Emitter

HTTP 20x, 30x
HTTP 40x
HTTP 50x

Frequently Asked Questions

What is the request lifecycle? +

The request lifecycle is the sequence of steps that happen from the moment a user makes an HTTP request until the server sends back a response.

What happens at the entry point of a request? +

The application bootstraps and loads configuration to create the Mezzio application instance, registers factories, aliases and delegators in the service container, reads all available routes with their allowed request methods and registers them (managed by FastRoute), and loads the predefined order of middleware in the pipeline.

How does routing work in a Mezzio-based application? +

FastRoute matches the incoming URL and method against the registered routes, for example matching a GET request to /page/about against the GetPageViewHandler handler under the route name page::about.

What happens during handler invocation? +

The matched route name is extracted from the request attribute and passed to the renderer, using code similar to $template = $request->getAttribute(RouteResult::class)->getMatchedRouteName();, after which the handler executes the custom business logic.

What happens during template rendering? +

Twig loads the matched template file, applies the layout it extends, renders its blocks, and includes any partials, producing the final HTML output.

How is the response created and returned to the browser? +

An HtmlResponse is created with a status code, headers, and the rendered HTML body. It then flows back through the middleware stack in reverse (the response pipeline), where middleware can modify headers, cookies, or compress content, before the response emitter sends the final response back to the browser as HTTP 20x/30x, 40x, or 50x.