marcguyer / version-middleware
PSR-7 middleware for managing routable versioning.
Requires
- php: ^7.1
- psr/container: ^1.0
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.1
- phpunit/phpunit: ^7.3
- psr/http-server-handler: ^1.0
- squizlabs/php_codesniffer: ^3.4
- webimpress/coding-standard: dev-master@dev
- zendframework/zend-coding-standard: dev-develop@dev
- zendframework/zend-diactoros: ^2.1
This package is auto-updated.
Last update: 2024-10-21 20:41:38 UTC
README
Provides version detection, making versioned resource routing possible in PSR-7 applications.
Installation
Install this library using composer:
$ composer require marcguyer/version-middleware
Composer will ask if you'd like to inject the ConfigProvider if you're using zendframework/zend-component-installer
. Answer yes or config it by hand.
Usage
Config
See the ConfigProvider for config defaults. You may override using the versioning
key. For example, the default version is 1
. You might release a new version and set the default version to 2
. Any clients not specifying a version via path or header will then be hitting version 2 resources.
Add to pipeline
Wire this middleware into your pipeline before routing. An example using a Zend Expressive pipeline:
... $app->pipe(ServerUrlMiddleware::class); ... $app->pipe(Psr7Versioning\VersionMiddleware::class); ... $app->pipe(RouteMiddleware::class); ...
Routing
Now, you can route based on the rewritten URI path. For example, in Expressive:
$app->get('/api/v1/ping', Api\Handler\PingHandler::class, 'api.ping'); $app->get('/api/v2/ping', Api\V2\Handler\PingHandler::class, 'api.v2.ping');
Namespaced version
Now, using the above routing example, assuming your v1 Ping is in namespace Api\Handler
, you may set the namespace for v2 Ping to be Api\V2\Handler
and extend the v1 handler. Any reference to services, models, other middleware will follow that namespace. Or, copy everything you want to be in the new version into a new namespace entirely.
Contributing
Docker Image
The Dockerfile
in the repo can be used to create a lightweight image locally for running tests and other composer scripts:
docker build --tag [your_chosen_image_name] .
Run tests
docker run --rm -it -v $(pwd):/app [your_chosen_image_name] composer test