PHP Class Zend\Expressive\Container\ApplicationFactory
This factory uses the following services, if available:
- 'Zend\Expressive\Router\RouterInterface'. If missing, an Aura router bridge
will be instantiated and used.
- 'Zend\Expressive\FinalHandler'. The service should be a callable to use as
the final handler when the middleware pipeline is exhausted.
- 'Zend\Diactoros\Response\EmitterInterface'. If missing, an EmitterStack is
created, adding a SapiEmitter to the bottom of the stack.
- 'config' (an array or ArrayAccess object). If present, and it contains route
definitions, these will be used to seed routes in the Application instance
before returning it.
When introspecting the
config service, the following structure can be used
to define routes:
return [
'routes' => [
[
'path' => '/path/to/match',
'middleware' => 'Middleware Service Name or Callable',
'allowed_methods' => [ 'GET', 'POST', 'PATCH' ],
'options' => [
'stuff' => 'to',
'pass' => 'to',
'the' => 'underlying router',
],
],
etc.
],
];
Each route MUST have a path and middleware key at the minimum.
The "allowed_methods" key may be omitted, can be either an array or the
value of the Zend\Expressive\Router\Route::HTTP_METHOD_ANY constant; any
valid HTTP method token is allowed, which means you can specify custom HTTP
methods as well.
The "options" key may also be omitted, and its interpretation will be
dependent on the underlying router used.
Furthermore, you can define middleware to pipe to the application to run on
every invocation (assuming they match and/or other middleware does not
return a response earlier). Use the following configuration:
return [
'middleware_pipeline' => [
An array of middleware to register prior to registration of the
routing middleware:
'pre_routing' => [
],
An array of middleware to register after registration of the
routing middleware:
'post_routing' => [
],
],
];
Each item in either the
pre_routing or
post_routing array must be an
array with the following specification:
[
required:
'middleware' => 'Name of middleware service, or a callable',
optional:
'path' => '/path/to/match',
'error' => true,
]
Note that the
path element can only be a literal.
error indicates
whether or not the middleware represents error middleware; this is done
so that Expressive can lazy-load an error middleware service (more below).
Omitting
error or setting it to a non-true value is the default,
indicating the middleware is standard middleware.
Middleware are pipe()'d to the application instance in the order in which
they appear. "pre_routing" middleware will execute before the application's
routing middleware, while "post_routing" middleware will execute afterwards.
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);
};
If you specify the middleware's
error flag as
true, the closure will
look like this:
function ($error, $request, $response, $next) 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($error, $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.
Show file
Open project: zendframework/zend-expressive
Public Methods
Method |
Description |
|
__invoke ( Interop\Container\ContainerInterface $container ) : Application |
Create and return an Application instance. |
|
Private Methods
Method Details
See the class level docblock for information on what services this
factory will optionally consume.
public __invoke ( Interop\Container\ContainerInterface $container ) : Application |
$container |
Interop\Container\ContainerInterface |
|
return |
Zend\Expressive\Application |
|