Middlewares are filters for HTTP requests. It filters the request using it’s parameters and redirect to different routes. If all filters passes then call the final controller.
Here’s a clear summary.
You can check my private git repo for working example: https://github.com/pdilip84/php2025/tree/main/middlewarenoilluminate
✅ 1. Middleware Definitions File
This file defines your middleware logic, like:
$checkAge = function ($request, Closure $next) {
if ($request['age'] < 18) {
return "You must be 18+";
}
return $next($request);
};
$logRequest = function ($request, Closure $next) {
echo "Logging request...\n";
return $next($request);
};
✅ 2. Middleware Pipeline Manager
This builds the pipeline (like Laravel’s internal Pipeline class):
function buildPipeline(array $middlewares, Closure $controller)
{
return array_reduce(
array_reverse($middlewares),
function ($next, $middleware) {
return function ($request) use ($middleware, $next) {
return $middleware($request, $next);
};
},
$controller
);
}
✅ 3. Controller (Final Destination)
The final action you want to take if all middleware passes:
$controller = function ($request) {
return "Welcome, {$request['user']}!";
};
✅ 4. Router / Entry Point
This is like a front controller. It:
- receives the request
- loads middlewares
- builds the pipeline
- runs it and returns response
$request = ['user' => 'Dilip', 'age' => 20];
// Load middleware
$middlewares = [$checkAge, $logRequest];
// Build and run pipeline
$pipeline = buildPipeline($middlewares, $controller);
$response = $pipeline($request);
echo $response;
✅ Summary Table
| File Type | Purpose |
|---|---|
| Middleware file | Defines middleware logic using closures |
| Pipeline manager | Builds and wraps middleware to call in sequence |
| Controller | Executes if all middleware pass |
| Entry/router | Receives the request, builds pipeline, shows response |
💡 Bonus: This mirrors Laravel’s lifecycle
Laravel just abstracts this with:
app/Http/Middleware/classes- A
Pipelineclass to manage chaining - A
RouteServiceProviderthat sends requests through middleware and to controllers
🛗 Middleware as a Lift (Elevator) – Simple Analogy
🧠 Imagine This:
- You enter a lift (elevator).
- You press some buttons for floors: 2, 4, 7.
- The lift stops at each floor, does something, and then continues up.
- When it reaches the top floor, you reach your destination (the controller).
🔁 How This Maps to Middleware
| Real World | Middleware System |
|---|---|
| Passenger | Incoming Request |
| Buttons pressed | Middlewares to be applied |
| Each floor stop | A middleware doing its task/check |
| Lift | The Middleware Manager / Pipeline |
| Final top floor | The Controller or final response handler |
🧾 Example Walkthrough
- The request gets in the lift.
- The lift stops at Floor 2 →
CheckAgemiddleware. - Then stops at Floor 4 →
CheckRolemiddleware. - Then Floor 7 →
Loggermiddleware. - Finally reaches Floor 10 → the controller returns the final response.
If any middleware (say on Floor 4) says “access denied”, the lift stops and doesn’t go further.
✅ Final Analogy Summary
Middleware is like an elevator ride.
- The request is the passenger.
- Each middleware is a floor where something is checked or processed.
- The controller is the final destination (top floor).
- The middleware manager is the lift that moves the request through all the middleware, in order.
