Middleware piped may be either callables or service names. Middleware
specified as services will be wrapped in a closure similar to the
following:
function ($request, $response, $next = null) use ($container, $middleware) {
$invokable = $container->get($middleware);
if (! is_callable($invokable)) {
throw new Exception\InvalidMiddlewareException(sprintf(
'Lazy-loaded middleware "%s" is not invokable',
$middleware
));
}
return $invokable($request, $response, $next);
};
This is done to delay fetching the middleware until it is actually used;
the upshot is that you will not be notified if the service is invalid to
use as middleware until runtime.
Middleware may also be passed as an array; each item in the array must
resolve to middleware eventually (i.e., callable or service name).
Finally, ensures that the route middleware is only ever registered
once.