|
| 1 | +# Request Lifecycle for a Mezzio-Based Application |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +The graph below shows how the request is handled by Dotkernel Light. |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +## Request Lifecycle Step-by-Step |
| 10 | + |
| 11 | +The following list describes what each of the steps from the graph does in Dotkernel Light. |
| 12 | + |
| 13 | +### 1. HTTP Request [public/index.php] |
| 14 | + |
| 15 | +- Bootstrap the application. |
| 16 | +- Load configuration. |
| 17 | +- Create the Mezzio application instance. |
| 18 | + |
| 19 | +### 2. Service Container |
| 20 | + |
| 21 | +Register: |
| 22 | + |
| 23 | +- Factories. |
| 24 | +- Aliases. |
| 25 | +- Delegators. |
| 26 | + |
| 27 | +All services are configured and ready to use. |
| 28 | + |
| 29 | +### 3. Route Registration |
| 30 | + |
| 31 | +Read all available routes with their allowed request methods and dynamically register them in the application. |
| 32 | +Routes are managed by FastRoute. |
| 33 | + |
| 34 | +#### Example |
| 35 | + |
| 36 | +| Item | Value | |
| 37 | +|------------|-----------------------------------| |
| 38 | +| Match | /page/about -> GetPageViewHandler | |
| 39 | +| Method | GET | |
| 40 | +| Route name | page::about | |
| 41 | + |
| 42 | +### 4. Middleware Pipeline [config/pipeline.php] |
| 43 | + |
| 44 | +Loads the predefined order of middleware. |
| 45 | +It defines how incoming HTTP requests move through the application and how responses are generated. |
| 46 | + |
| 47 | +### 5. Routing |
| 48 | + |
| 49 | +FastRoute matches the URL and method against registered routes. |
| 50 | + |
| 51 | +#### Example |
| 52 | + |
| 53 | +| Item | Value | |
| 54 | +|------------|--------------------| |
| 55 | +| Match | GET /page/about | |
| 56 | +| Handler | GetPageViewHandler | |
| 57 | +| Route name | page::about | |
| 58 | + |
| 59 | +### 6. Handler Invocation |
| 60 | + |
| 61 | +Extract the matched route name from the request and pass it to the renderer. |
| 62 | + |
| 63 | +```php |
| 64 | +$template = $request->getAttribute(RouteResult::class)->getMatchedRouteName(); |
| 65 | +// $template = 'page::about'; |
| 66 | +``` |
| 67 | + |
| 68 | +### 7. Custom Logic Execution in Handler |
| 69 | + |
| 70 | +Execute the business logic in the handler. |
| 71 | +The process can involve services and any custom logic. |
| 72 | + |
| 73 | +### 8. Template Rendering |
| 74 | + |
| 75 | +Twig loads the template, applies layout, renders blocks and includes partials. |
| 76 | + |
| 77 | +#### Example |
| 78 | + |
| 79 | +| Item | Value | |
| 80 | +|------------------|-----------------------------------------| |
| 81 | +| Load | src/Page/templates/page/about.html.twig | |
| 82 | +| Extends | @layout/default.html.twig | |
| 83 | +| Render blocks | title, content | |
| 84 | +| Include partials | alerts.html.twig, etc. | |
| 85 | +| Output | Final HTML | |
| 86 | + |
| 87 | +### 9. Response Creation |
| 88 | + |
| 89 | +An HtmlResponse is created with status, headers and the rendered HTML body. |
| 90 | + |
| 91 | +#### Example |
| 92 | + |
| 93 | +| Item | Value | |
| 94 | +|--------------|--------------------------| |
| 95 | +| Status | 200 OK | |
| 96 | +| Content-Type | text/html; charset=utf-8 | |
| 97 | +| Body | Rendered HTML | |
| 98 | + |
| 99 | +### 10. Response Pipeline |
| 100 | + |
| 101 | +The response flows back through the middleware stack. |
| 102 | +Middleware can modify headers, cookies, compress content, etc. |
| 103 | + |
| 104 | +### 11. Response Emitter |
| 105 | + |
| 106 | +The final response is sent back to the browser. |
| 107 | +The page is rendered and sent to the user. |
0 commit comments