Laravel Request Lifecycle
The Laravel request lifecycle begins when a user sends an HTTP request to the web server (Apache, Nginx). The server forwards the request to the public/index.php
file, the application's single entry point. This file loads Composer’s autoloader, performs essential bootstrapping, and constructs a new instance of the Laravel application.
The next step is the HTTP Kernel, which manages the request, response, and middleware stacks. The global middleware stack runs first, acting as filters for every HTTP request. Service providers register core services, and after bootstrapping, the router dispatches the request to a route or controller, executing any route-specific middleware.
Controllers (or route closures) process the request and generate a response—HTML view, JSON, download, or redirect. Before the response is sent to the user's browser, it passes back out through the middleware stack for any post-processing. Finally, the Kernel’s handle()
method returns the response for index.php
to deliver to the browser.
Middleware's Role in the Lifecycle
Middleware in Laravel act as filters operating before or after the core route/controller logic. They check authentication, log requests, manipulate headers, etc. Middleware can be global (applied to all requests) or route-specific (applied only to selected routes). Middleware can halt, redirect, modify requests, or even process responses before they're sent back to the client.
Creating Custom Middleware
Global Middleware
- Generate middleware:
php artisan make:middleware CustomGlobalMiddleware
.[8] - Add logic in
app/Http/Middleware/CustomGlobalMiddleware.php
:
public function handle($request, Closure $next)
{
// Custom logic here
return $next($request);
}
- Register in
app/Http/Kernel.php
by adding to the$middleware
array:
protected $middleware = [
// ... existing middleware,
\App\Http\Middleware\CustomGlobalMiddleware::class,
];
- Now the middleware runs for every HTTP request.
Route Middleware
- Generate middleware:
php artisan make:middleware CustomRouteMiddleware
. - Add logic in
app/Http/Middleware/CustomRouteMiddleware.php
as above. - Register in
app/Http/Kernel.php
within$routeMiddleware
:
protected $routeMiddleware = [
'custom' => \App\Http\Middleware\CustomRouteMiddleware::class,
];
- Apply to route:
Route::get('/example', function () {
return 'Example Route';
})->middleware('custom');
- For groups:
Route::middleware(['custom'])->group(function () {
Route::get('/a', ...);
Route::get('/b', ...);
});
Passing Parameters to Middleware
Parameters can be sent to route middleware by appending them after a colon in the route definition:
Route::get('/post/{id}', 'PostController@show')->middleware('verify.role:admin');
In the middleware class, receive them as extra arguments:
public function handle($request, Closure $next, $role)
{
// Check for required role
return $next($request);
}
To access route parameters (like {id}
) inside middleware:
$id = $request->route('id'); // Gets route param 'id'
$params = $request->route()->parameters(); // All parameters array
This lets you make your middleware context-aware and dynamic.
Quick Reference Table: Request Lifecycle Steps
Step | Purpose |
---|---|
Web Server | Receives & forwards HTTP request |
index.php | Application entry, bootstraps Laravel |
Kernel | Handles requests, global middleware |
Service Providers | Register services |
Router | Routes requests, applies route middleware |
Controller/View | Core logic & generates response |
Middleware | Filters/Processes requests & responses |
Response | Sent back via Kernel & index.php |
This sequence gives a robust, hands-on understanding of where middleware intercepts requests, how to build custom ones, and how parameters integrate with the lifecycle—all as essential for mastering Laravel’s request flow and middleware.
Top comments (0)