Elevator-Buttons

Middleware – broken it down exactly like a real framework would

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 TypePurpose
Middleware fileDefines middleware logic using closures
Pipeline managerBuilds and wraps middleware to call in sequence
ControllerExecutes if all middleware pass
Entry/routerReceives the request, builds pipeline, shows response

💡 Bonus: This mirrors Laravel’s lifecycle

Laravel just abstracts this with:

  • app/Http/Middleware/ classes
  • A Pipeline class to manage chaining
  • A RouteServiceProvider that 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 WorldMiddleware System
PassengerIncoming Request
Buttons pressedMiddlewares to be applied
Each floor stopA middleware doing its task/check
LiftThe Middleware Manager / Pipeline
Final top floorThe Controller or final response handler

🧾 Example Walkthrough

  1. The request gets in the lift.
  2. The lift stops at Floor 2CheckAge middleware.
  3. Then stops at Floor 4CheckRole middleware.
  4. Then Floor 7Logger middleware.
  5. 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.