Request Lifecycle Overview
The complete Magento request lifecycle from HTTP request to response.
Lifecycle steps:
1. HTTP Request Arrives
2. Bootstrap (App\Bootstrap)
3. Application initialization
4. Area determination
5. Router matching
6. Controller instantiation
7. Action execution
8. Result processing
9. Layout rendering
10. HTTP Response
Step 1: HTTP Request:
GET /catalog/product/view/id/5 HTTP/1.1
Host: example.com
Accept: text/html
Step 2: Bootstrap:
// pub/index.php
require __DIR__ . '/../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(
BP,
$_SERVER
);
$bootstrap->run(
$this->_objectManager->create(
\Magento\Framework\App\Http\InterfaceResolver::class
)
);
Step 3: Application init:
ObjectManager created
Config loaded
Modules registered
Area determined (frontend/adminhtml/etc)
Routing and Controller Resolution
How URLs are matched to controllers.
Step 4: Area determination:
// Based on URL prefix or configuration
$area = 'frontend'; // Default
// Admin URLs → adminhtml
// REST API → webapi_rest
// Cron → crontab
Step 5: Router matching:
1. UrlRewriteRouter checks url_rewrite table
2. DefaultRouter checks routes.xml
3. Custom routers try to match
4. First match returns action class
URL: /catalog/product/view/id/5
→ FrontName: catalog
→ Controller: product
→ Action: view
→ Class: Magento\Catalog\Controller\Product\View
Step 6: Controller instantiation:
// ObjectManager creates controller
$controller = $objectManager->create(
\Magento\Catalog\Controller\Product\View::class
);
// Dependencies injected via constructor
Step 7: Action execution:
// FrontController calls execute()
$result = $controller->execute();
// Controller processes request
// Returns ResultInterface
Result Processing and Rendering
How results are processed into HTTP responses.
Step 8: Result processing:
// Result object returned
$result = $controller->execute();
// For Page result:
// - Layout is loaded
// - Blocks are created
// - Handle updates applied
// For Json result:
// - Data is encoded
// - Content-Type header set
// For Redirect result:
// - Location header set
// - Status code 302
Step 9: Layout rendering (for Page result):
1. Layout XML loaded
2. Handles merged
3. Blocks created
4. Blocks rendered
5. HTML assembled
Layout rendering steps:
1. Generate layout from XML
2. Apply handle updates
3. Create block instances
4. Call _toHtml() on blocks
5. Assemble final HTML
6. Apply page template
Step 10: HTTP Response:
// Response headers
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Cache-Control: no-cache, no-store
Set-Cookie: PHPSESSID=...
// Response body
<!DOCTYPE html>
<html>
<head>...</head>
<body>...</body>
</html>
Lifecycle Hooks and Events
Events fired during the request lifecycle.
Key lifecycle events:
1. application_front_init — Front controller initialized
2. controller_action_predispatch — Before action execution
3. controller_action_postdispatch — After action execution
4. layout_load_before — Before layout loaded
5. layout_generate_blocks_after — After blocks created
6. layout_render_before — Before rendering
7. http_response_send_before — Before response sent
Event observers:
<event name="controller_action_predispatch">
<observer name="vendor_log_request" instance="Vendor\Module\Observer\RequestLogger"/>
</event>
<event name="http_response_send_before">
<observer name="vendor_add_headers" instance="Vendor\Module\Observer\ResponseHeaders"/>
</event>
Performance critical points:
1. Bootstrap (ObjectManager creation)
2. Configuration loading
3. Router matching
4. Controller instantiation (DI)
5. Layout generation
6. Block rendering
Debugging lifecycle:
// Add to controller
$startTime = microtime(true);
// ... action logic
$endTime = microtime(true);
$this->logger->info('Controller time: ' . ($endTime - $startTime));
// Check total request time in response header
$this->getResponse()->setHeader('X-Request-Time',
round($endTime - $startTime, 4) . 's'
);
Lifecycle optimization:
1. Use compiled DI (setup:di:compile)
2. Enable full-page cache
3. Use flat catalog (if applicable)
4. Minimize layout handles
5. Use block cache
Quiz
1. What is the first step in the Magento request lifecycle?
2. What event fires before controller execution?
3. What determines the area (frontend/adminhtml)?
4. What happens during layout rendering?
Flashcards
Question
What are the 10 lifecycle steps?
Click to reveal answer
Answer
HTTP → Bootstrap → Init → Area → Router → Controller → Action → Result → Render → Response
Question
What event fires before action execution?
Click to reveal answer
Answer
controller_action_predispatch
Question
What is the first lifecycle step?
Click to reveal answer
Answer
Bootstrap (App\Bootstrap)
Question
What renders layout to HTML?
Click to reveal answer
Answer
Block _toHtml() methods
Question
What event fires before response is sent?
Click to reveal answer
Answer
http_response_send_before
Revision Notes
Key Takeaways
- 1. Lifecycle: HTTP → Bootstrap → Init → Area → Router → Controller → Action → Result → Render → Response
- 2. Bootstrap initializes ObjectManager and application
- 3. Area determines which configuration loads
- 4. Router matches URL to controller class
- 5. Controller execute() returns ResultInterface
- 6. Layout rendering generates HTML from blocks
- 7. Events fire at each lifecycle stage
Interview Tips
- • Walk through the complete request lifecycle
- • Explain where performance bottlenecks occur
- • Describe how to debug lifecycle issues
- • Discuss optimization strategies
Cheat Sheet
Request Lifecycle Cheat Sheet
Steps:
- HTTP Request arrives
- Bootstrap (ObjectManager)
- App initialization
- Area determination
- Router matching
- Controller instantiation
- Action execution (execute())
- Result processing
- Layout rendering
- HTTP Response
Key events:
- application_front_init
- controller_action_predispatch
- controller_action_postdispatch
- layout_load_before
- http_response_send_before
Performance:
- Compiled DI
- Full-page cache
- Block cache
- Flat catalog