© 2026 Laravel

Laravel Request Lifecycle là gì? Hiểu sâu để trở thành Senior Laravel Developer

4 phút đọc 12 lượt xem
#laravel #request lifecycle #middleware #service container #architecture

Nếu bạn chỉ dùng Laravel ở mức:

  • Viết controller
  • Gọi model
  • Return response

Thì bạn mới chỉ đang “dùng framework”.

Nhưng nếu bạn hiểu Request Lifecycle, bạn sẽ:

  • Debug nhanh hơn
  • Optimize đúng chỗ
  • Customize framework theo ý mình
  • Và quan trọng nhất: bước sang level senior

#Laravel Request Lifecycle là gì?

Request Lifecycle là toàn bộ quá trình:

Từ khi một HTTP request đi vào hệ thống → cho đến khi trả về response cho client.

Hiểu đơn giản:

User → HTTP Request → Laravel → Response → User

Nhưng bên trong Laravel, flow này phức tạp và rất “magic”.

#Tổng quan flow Laravel

Luồng xử lý trong Laravel (đơn giản hóa):

  1. public/index.php
  2. Bootstrap application
  3. Load Service Container
  4. Load Service Providers
  5. Handle HTTP Kernel
  6. Middleware xử lý request
  7. Route matching
  8. Controller / Closure
  9. Return Response
  10. Middleware xử lý response

#Entry Point – public/index.php

Mọi request đều đi qua:

public/index.php

Đây là nơi:

  • Load autoload (vendor/autoload.php)
  • Bootstrap Laravel app
$app = require_once __DIR__.'/../bootstrap/app.php';

Đây là “cổng vào duy nhất” của Laravel.

#Bootstrap Application

File:

bootstrap/app.php

Tại đây Laravel:

  • Khởi tạo Application instance
  • Bind core services vào container

Đây chính là lúc Service Container bắt đầu hoạt động.

#Service Container – Trái tim của Laravel

Laravel sử dụng Dependency Injection Container.

Ví dụ:

public function __construct(UserService $service)

Laravel tự động:

  • Resolve class
  • Inject dependency

Điều này xảy ra trong lifecycle, không phải magic.

#Service Providers – Nơi đăng ký hệ thống

Laravel load tất cả providers:

config/app.php

Hoặc auto-discovery trong Laravel 12.

Service Provider có 2 phần:

public function register()
public function boot()
  • register() → bind service
  • boot() → chạy logic sau khi bind xong

Sai lầm phổ biến:

  • Bind logic vào boot → khó control lifecycle

#HTTP Kernel – Trung tâm xử lý

File:

app/Http/Kernel.php

Kernel nhận request:

$response = $kernel->handle($request);

Kernel sẽ:

  • Đẩy request qua middleware
  • Dispatch đến router

#Middleware – Lớp filter cực kỳ quan trọng

Middleware giống như “layer” xử lý request.

Ví dụ:

  • Auth
  • Logging
  • Rate limit

Flow:

Request → Middleware → Middleware → Controller → Middleware → Response

Ví dụ:

public function handle($request, Closure $next)
{
    // before
    $response = $next($request);
    // after

    return $response;
}

Senior mindset:

  • Middleware là nơi cross-cutting concerns
  • Không nên nhét vào controller

#Routing – Mapping request

Laravel match route:

routes/web.php
routes/api.php

Ví dụ:

Route::get('/users', [UserController::class, 'index']);

Laravel dùng:

  • Fast route matching
  • Cached route (production)

#Controller / Action

Khi match thành công:

Laravel gọi controller:

UserController@index

Dependency injection tiếp tục hoạt động:

public function index(UserService $service)

Đây là lúc business logic bắt đầu.

#Response – Trả dữ liệu về client

Laravel convert response:

  • String → HTTP response
  • Array → JSON
  • View → HTML

Ví dụ:

return response()->json($data);

#Middleware (Response Phase)

Sau khi controller chạy xong:

Middleware chạy ngược lại:

Controller → Middleware → Middleware → Client

Đây là nơi:

  • Modify response
  • Add headers
  • Logging

#Sơ đồ tổng thể

Request
index.php
bootstrap/app.php
Service Container
Service Providers
HTTP Kernel
Middleware (before)
Router
Controller
Response
Middleware (after)
Client

#Mindset – Hiểu để làm gì?

Junior:

Laravel chạy được là ok

Senior:

Request đang đi qua đâu? Có thể optimize ở đâu?

Ví dụ thực tế:

  • App chậm → check middleware
  • Memory leak → check container binding
  • Bug khó hiểu → debug lifecycle

#Sai lầm phổ biến

  • Không hiểu middleware chạy 2 chiều
  • Nhầm lẫn register vs boot
  • Lạm dụng Service Container (over abstraction)
  • Không dùng route cache

#Kết luận

Hiểu Request Lifecycle giúp bạn:

  • Debug nhanh hơn
  • Tối ưu performance đúng chỗ
  • Viết code theo kiến trúc tốt hơn