On this page
Reference
Migration to Mezzio 3.0
Mezzio 3.0 should not result in many upgrade problems for users. However, starting in this version, we offer a few changes affecting the following that you should be aware of, and potentially update your application to adopt:
- PHP 7.1 support
- PSR-15 support
- New dependencies
- New features
- Signature and behavior changes
- Removed classes and traits
- Upgrading from v2
PHP 7.1 support
Starting in Mezzio 3.0 we support only PHP 7.1+.
PSR-15 Support
All middleware and delegators now implement interfaces from PSR-15 instead of http-interop/http-middleware (a PSR-15 precursor). This means the following changes were made throughout Mezzio:
-
The
process()method of all middleware now type hint the second argument against the PSR-15RequestHandlerInterface, instead of the previousDelegateInterface. -
The
process()method of all middleware now have a return type hint of\Psr\Http\Message\ResponseInterface. -
All "delegators" have become request handlers: these now implement the PSR-15 interface
RequestHandlerInterfaceinstead of the formerDelegateInterface. -
The
process()method of handlers (formerly delegators) have been renamed tohandle()and given a return type hint of\Psr\Http\Message\ResponseInterface.
This change also affects all middleware you, as an application developer, have written, and your middleware will need to be updated. We provide a tool for this via mezzio-tooling. Make sure that package is up-to-date (a version 1 release should be installed), and run the following:
$ ./vendor/bin/mezzio migrate:interop-middleware
This tool will locate any http-interop middleware and update it to PSR-15 middleware.
New dependencies
Mezzio adds the following packages as dependencies:
-
psr/http-server-middleware provides the PSR-15 interfaces, and replaces the previous dependency on http-interop/http-middleware.
-
mezzio/mezzio-router; previously, we depended on this package indirectly; now it is a direct requirement.
-
mezzio/mezzio-tooling; this was suggested previously, but is now required as a development dependency.
-
laminas/laminas-httphandlerrunner; this is now used for the purposes of marshaling the server request, dispatching the application, and emitting the response. The functionality is generalized enough to warrant a separate package.
New features
The following classes were added in version 3:
-
Mezzio\Container\ApplicationConfigInjectionDelegatoris a delegator factory capable of piping and routing middleware from configuration. See the recipe on autowiring routes and pipeline middleware for more information. -
Mezzio\Container\ApplicationPipelineFactorywill produce an emptyMiddlewarePipefor use withMezzio\Application. -
Mezzio\Container\EmitterFactorywill produce aLaminas\HttpHandlerRunner\Emitter\EmitterStackinstance for use with theRequestHandlerRunnerinstance composed by theApplication. See the chapter on emitters for more details. -
Mezzio\Container\MiddlewareContainerFactorywill produce aMiddlewareContainercomposing the application container instance. -
Mezzio\Container\MiddlewareFactoryFactorywill produce aMiddlewareFactorycomposing aMiddlewareContainerinstance. -
Mezzio\Container\RequestHandlerRunnerFactorywill produce aLaminas\HttpHandlerRunner\RequestHandlerRunnerinstance for use with theApplicationinstance. See the laminas-httphandlerrunner documentation for more details on this collaborator. -
Mezzio\Container\ServerRequestErrorResponseGeneratorFactorywill produce aMezzio\Response\ServerRequestErrorResponseGeneratorinstance for use with theRequestHandlerRunner. -
Mezzio\Container\ServerRequestFactoryFactorywill produce a PHP callable capable of generating a PSR-7ServerRequestInterfaceinstance for use with theRequestHandlerRunner. -
Mezzio\MiddlewareContainerdecorates a PSR-11 container, and ensures that the values pulled are PSR-15MiddlewareInterfaceinstances. If the container returns a PSR-15RequestHandlerInterface, it decorates it viaLaminas\Stratigility\Middleware\RequestHandlerMiddleware. All other types result in an exception being thrown. -
Mezzio\MiddlewareFactoryallows creation ofMiddlewareInterfaceinstances from a variety of argument types, and is used byApplicationto allow piping and routing to middleware services, arrays of services, and more. It composes aMiddlewareContainerinternally. -
Mezzio\Response\ServerRequestErrorResponseGeneratorcan act as a response generator for theRequestHandlerRunnerwhen its composed server request factory raises an exception.
Signature and behavior changes
The following signature changes were made that could affect class extensions and/or consumers.
Application
Mezzio\Application was refactored dramatically for version 3.
If you were instantiating it directly previously, the constructor arguments are now, in order:
Mezzio\MiddlewareFactoryLaminas\Stratigility\MiddlewarePipeInterfaceMezzio\Router\RouteCollectorLaminas\HttpHandlerRunner\RequestHandlerRunnerMezzio\Application::__construct(...)
Application no longer supports piping or routing to double-pass middleware. If
you continue to need double-pass middleware (e.g., defined by a third-party
library), use Laminas\Stratigility\doublePassMiddleware() to decorate it prior to
piping or routing to it:
use Laminas\Diactoros\Response;
use function Laminas\Stratigility\doublePassMiddleware;
$app->pipe(doublePassMiddleware($someDoublePassMiddleware, new Response()));
$app->get('/foo', doublePassMiddleware($someDoublePassMiddleware, new Response()));
Additionally, the following methods were removed:
pipeRoutingMiddleware(): usepipe(\Mezzio\Router\Middleware\RouteMiddleware::class)instead.pipeDispatchMiddleware(): usepipe(\Mezzio\Router\Middleware\DispatchMiddleware::class)instead.getContainer()getDefaultDelegate(): ensure you pipe middleware or a request handler capable of returning a response at the innermost layer;Mezzio\Handler\NotFoundHandlercan be used for this.getEmitter(): use theLaminas\HttpHandlerRunner\Emitter\EmitterInterfaceservice from the container.injectPipelineFromConfig(): use the newApplicationConfigInjectionDelegatorand/or the static method of the same name it defines.injectRoutesFromConfig(): use the newApplicationConfigInjectionDelegatorand/or the static method of the same name it defines.
ApplicationFactory
Mezzio\Container\ApplicationFactory no longer looks at the
mezzio.programmatic_pipeline flag, nor does it inject pipeline
middleware and/or routed middleware from configuration any longer.
If you want to use configuration-driven pipelines and/or middleware, you may
register the new class Mezzio\Container\ApplicationConfigInjectionDelegator
as a delegator factory on the Mezzio\Application service.
NotFoundHandlerFactory
Mezzio\Container\NotFoundHandlerFactory now returns an instance of
Mezzio\Handler\NotFoundHandler, instead of
Mezzio\Middleware\NotFoundHandler (which has been removed).
LazyLoadingMiddleware
Mezzio\Middleware\LazyLoadingMiddleware now composes a
Mezzio\MiddlewareContainer instance instead of a more general PSR-11
container; this is to ensure that the value returned is a PSR-15
MiddlewareInterface instance.
Removed classes and traits
-
Mezzio\AppFactorywas removed. If you were using it previously, either useMezzio\Applicationdirectly, or aLaminas\Stratigility\MiddlewarePipeinstance. -
Mezzio\ApplicationConfigInjectionTrait; the functionality of this trait was replaced by theMezzio\Container\ApplicationConfigInjectionDelegator. -
Mezzio\Delegate\NotFoundDelegate; useMezzio\Handler\NotFoundHandlerinstead. Its factory,Mezzio\Container\NotFoundDelegateFactory, was also removed. -
Mezzio\Emitter\EmitterStack; useLaminas\HttpHandlerRunner\Emitter\EmitterStackinstead. -
Mezzio\IsCallableInteropMiddlewareTrait; there is no functional equivalent, nor a need for this functionality as of version 3. -
Mezzio\MarshalMiddlewareTrait; the functionality of this trait was replaced by a combination ofMezzio\MiddlewareContainerandMezzio\MiddlewareFactory. -
Mezzio\Middleware\DispatchMiddleware; useMezzio\Router\Middleware\DispatchMiddlewareinstead. -
Mezzio\Middleware\ImplicitHeadMiddleware; useMezzio\Router\Middleware\ImplicitHeadMiddlewareinstead. -
Mezzio\Middleware\ImplicitOptionsMiddleware; useMezzio\Router\Middleware\ImplicitOptionsMiddlewareinstead. -
Mezzio\Middleware\NotFoundHandler; useMezzio\Handler\NotFoundHandlerinstead. -
Mezzio\Middleware\RouteMiddleware; useMezzio\Router\Middleware\RouteMiddlewareinstead.
Upgrading
We provide a package you can add to your existing v2 application in order to upgrade it to version 3.
Before installing and running the migration tooling, make sure you have checked in your latest changes (assuming you are using version control), or have a backup of your existing code.
Install the migration tooling using the following command:
$ composer require --dev mezzio/mezzio-migration
Once installed, run the following command to migrate your application:
$ ./vendor/bin/mezzio-migration migrate
This package does the following:
- Uninstalls all current dependencies (by removing the
vendor/directory). - Updates existing dependency constraints for known Mezzio packages to their latest stable versions. (See the tools README for details on what versions of which packages the tool uses.)
- Adds development dependencies on laminas/laminas-component-installer and mezzio/mezzio-tooling.
- Updates the
config/pipeline.phpfile to:- add strict type declarations.
- modify it to return a callable, per the v3 skeleton.
- update the middleware pipeline as follows:
pipeRoutingMiddleware()becomes apipe()operation referencing the mezzio-routerRouteMiddleware.pipeDispatchMiddleware()becomes apipe()operation referencing the mezzio-routerDispatchMiddleware.- update references to
ImplicitHeadMiddlewareto reference the version in mezzio-router. - update references to
ImplicitOptionsMiddlewareto reference the version in mezzio-router. - update references to
Mezzio\Middleware\NotFoundHandlerto referenceMezzio\Handler\NotFoundHandler. - add a
pipe()entry for the mezzio-routerMethodNotAllowedMiddleware.
- Updates the
config/routes.phpfile to:- add strict type declarations.
- modify it to return a callable, per the v3 skeleton.
- Replaces the
public/index.phpfile with the latest version from the skeleton. - Updates
config/container.phpwhen Pimple or Aura.Di are in use:- For Pimple:
- The package
xtreamwayz/pimple-container-interopis replaced withlaminas/laminas-pimple-config. - The Pimple variant of
container.phpfrom the v3 skeleton is used.
- The package
- For Aura.Di
- The package
aura/diis replaced withlaminas/laminas-auradi-config. - The Aura.Di variant of
container.phpfrom the v3 skeleton is used.
- The package
- For Pimple:
- Executes
./vendor/bin/mezzio migrate:interop-middleware. - Executes
./vendor/bin/mezzio migrate:middleware-to-request-handler. - Runs
./vendor/bin/phpcbfif it is installed.
These steps should take care of most migration tasks.
It does not update unit tests. These cannot be automatically updated, due to the amount of variance in testing strategies.
When done, use a diffing tool to compare and verify all changes. Please be aware that the tool is not designed for edge cases; there may be things it does not do or cannot catch within your code. When unsure, refer to the other sections in this document to determine what else you may need to change.