Provided Factories

Expressive provides several factories compatible with PSR-11 Container to facilitate setting up common dependencies. The following is a list of provided factories, what they will create, the suggested service name, and any additional dependencies they may require.

zend-expressive

The zend-expressive package ships Zend\Expressive\ConfigProvider, which defines configuration that references each of these factories, using the suggested names; this provider is registered by default when using the skeleton application.

All factories, unless noted otherwise, are in the Zend\Expressive\Container namespace, and define an __invoke() method that accepts an Psr\Container\ContainerInterface instance as the sole argument.

ApplicationFactory

ApplicationPipelineFactory

EmitterFactory

This factory creates an instance of Zend\HttpHandlerRunner\Emitter\EmitterStack, pushing a Zend\HttpHandlerRunner\Emitter\SapiEmitter to it.

ErrorHandlerFactory

ErrorResponseGeneratorFactory

When the config service is present, the factory can utilize two values:

As an example:

'debug' => true,
'zend-expressive' => [
    'error_handler' => [
        'template_error' => 'name of error template',
    ],
],

MiddlewareContainerFactory

This factory returns an instance of Zend\Expressive\MiddlewareContainer injected with the container instance itself.

The MiddlewareContainer is a PSR-11 container itself, but ensures that instances pulled are PSR-15 MiddlewareInterface instances:

MiddlewareFactoryFactory

The MiddlewareFactory is used by Zend\Expressive\Application to prepare $middleware arguments to pipe(), route(), et al, ensuring they are MiddlewareInterface implementations. It handles the following types:

NotFoundHandlerFactory

When the config service is present, the factory can utilize two values:

As an example:

'zend-expressive' => [
    'error_handler' => [
        'template_404' => 'name of 404 template',
    ],
],

RequestHandlerRunnerFactory

This factory generates the RequestHandlerRunner instance used by the Application instance to "run" the application. It marshals a request instance, passes it to the application pipeline to handle, and emits the returned response. If an error occurs during request generation, it uses the ServerRequestErrorResponseGenerator to generate the response to emit.

ResponseFactoryFactory

By default, this uses zend-diactoros to produce a response, and will raise an exception if that package is not installed. You can provide an alternate factory if you want to use an alternate PSR-7 implementation.

ServerRequestErrorResponseGeneratorFactory

When the config service is present, the factory can utilize two values:

As an example:

'debug' => true,
'zend-expressive' => [
    'error_handler' => [
        'template_error' => 'name of error template',
    ],
],

ServerRequestFactoryFactory

By default, this uses zend-diactoros to produce a request, and will raise an exception if that package is not installed. You can provide an alternate factory if you want to use an alternate PSR-7 implementation.

StreamFactoryFactory

By default, this uses zend-diactoros to produce a stream, and will raise an exception if that package is not installed. You can provide an alternate factory if you want to use an alternate PSR-7 implementation.

WhoopsErrorResponseGeneratorFactory

WhoopsFactory

This factory creates and configures a Whoops\Run instance so that it will work properly with Zend\Expressive\Application; this includes disabling immediate write-to-output, disabling immediate quit, etc. The PrettyPageHandler returned for the Zend\Expressive\WhoopsPageHandler service will be injected.

It consumes the following config structure:

'whoops' => [
    'json_exceptions' => [
        'display'    => true,
        'show_trace' => true,
        'ajax_only'  => true,
    ],
],

If no whoops top-level key is present in the configuration, a default instance with no JsonResponseHandler composed will be created.

WhoopsPageHandlerFactory

It consumes the following config structure:

'whoops' => [
    'editor' => 'editor name, editor service name, or callable',
],

The editor value must be a known editor name (see the Whoops documentation for pre-configured editor types), a callable, or a service name to use.

zend-expressive-router

The zend-expressive-router package ships Zend\Expressive\Router\ConfigProvider, which defines configuration that references each of these factories, using the suggested names; this provider is registered by default when using the skeleton application.

Individual router implementation packages are expected to provide the Zend\Expressive\Router\RouterInterface service.

All factories listed below are under the Zend\Expressive\Router\Middleware namespace (unless otherwise specified), and define an __invoke() method that accepts a Psr\Container\ContainerInterface instance as the sole argument.

DispatchMiddlewareFactory

ImplicitHeadMiddlewareFactory

ImplicitOptionsMiddlewareFactory

MethodNotAllowedMiddlewareFactory

RouteCollectorFactory

RouteMiddlewareFactory

Factories provided by template engine packages

The following factories are provided by individual template engine packages. Generally speaking, these will be provided to your container configuration during installation.

PlatesRendererFactory

It consumes the following config structure:

'templates' => [
    'extension' => 'file extension used by templates; defaults to html',
    'paths' => [
        // namespace / path pairs
        //
        // Numeric namespaces imply the default/main namespace. Paths may be
        // strings or arrays of string paths to associate with the namespace.
    ],
]

One note: Due to a limitation in the Plates engine, you can only map one path per namespace when using Plates.

TwigRendererFactory

It consumes the following config structure:

'debug' => boolean,
'templates' => [
    'cache_dir' => 'path to cached templates',
    'assets_url' => 'base URL for assets',
    'assets_version' => 'base version for assets',
    'extension' => 'file extension used by templates; defaults to html.twig',
    'paths' => [
        // namespace / path pairs
        //
        // Numeric namespaces imply the default/main namespace. Paths may be
        // strings or arrays of string paths to associate with the namespace.
    ],
]

When debug is true, it disables caching, enables debug mode, enables strict variables, and enables auto reloading. The assets_* values are used to seed the TwigExtension instance (assuming the router was found).

ZendViewRendererFactory

It consumes the following config structure:

'templates' => [
    'layout' => 'name of layout view to use, if any',
    'map'    => [
        // template => filename pairs
    ],
    'paths'  => [
        // namespace / path pairs
        //
        // Numeric namespaces imply the default/main namespace. Paths may be
        // strings or arrays of string paths to associate with the namespace.
    ],
]

When creating the PhpRenderer instance, it will inject it with a Zend\View\HelperPluginManager instance (either pulled from the container, or instantiated directly). It injects the helper plugin manager with custom url and serverurl helpers, Zend\Expressive\ZendView\UrlHelper and Zend\Expressive\ZendView\ServerUrlHelper, respectively.