src/Router.php
private function runControllerThroughMiddleware(
        array $middleware,
        ServerRequestInterface $request,
        Closure $controllerRunner,
        $i = 0
    ) {
        if (isset($middleware[$i + 1])) {
            $next = function (ServerRequestInterface $request) use ($middleware, $controllerRunner, $i) {
                return $this->runControllerThroughMiddleware($middleware, $request, $controllerRunner, $i + 1);
            };
        } else {
            $next = $controllerRunner;
        }

        if (is_callable($middleware[$i])) {
            return $middleware[$i]($request, $next);
        }

        if (is_subclass_of($middleware[$i], Middleware::class)) {
            if (is_string($middleware[$i])) {
                $middleware[$i] = new $middleware[$i];
            }

            return $middleware[$i]->handle($request, $next);
        }

        throw new InvalidMiddlewareException('Invalid middleware for route: '.$this->currentRoute);
    }
private function runControllerThroughMiddleware(
        array $middleware,
        ServerRequestInterface $request,
        Closure $controllerRunner,
        $i = 0
    ) {
        if (isset($middleware[$i + 1])) {
            $next = function (ServerRequestInterface $request) use ($middleware, $controllerRunner, $i) {
                return $this->runControllerThroughMiddleware($middleware, $request, $controllerRunner, $i + 1);
            };
        } else {
            $next = $controllerRunner;
        }

        if (is_callable($middleware[$i])) {
            return $middleware[$i]($request, $next);
        }

        if (is_subclass_of($middleware[$i], Middleware::class)) {
            if (is_string($middleware[$i])) {
                $middleware[$i] = new $middleware[$i];
            }

            return $middleware[$i]->handle($request, $next);
        }

        throw new InvalidMiddlewareException('Invalid middleware for route: '.$this->currentRoute);
    }
Run the controller through the middleware (list)

@param Middleware[] $middleware
@param ServerRequestInterface $request
@param Closure $controllerRunner
@param int $i
@return ResponseInterface|null
@throws InvalidMiddlewareException