linkeddatacenter / url-rewriter
Middleware to change request uri.
Requires
- php: ^7.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.1
- middlewares/utils: ^2.1
- phpunit/phpunit: ^6.0|^7.0
- squizlabs/php_codesniffer: ^3.0
- zendframework/zend-diactoros: ^1.3
This package is auto-updated.
Last update: 2024-01-23 18:06:04 UTC
README
Simple Middleware to rewrite the path, the query and the fragment of the an http request uri. It requires an array of rules that are evaluated in sequence. A rule is a two position array: array[0] is the regexp pattern to search (internally translated in #^$pattern$# and array[1] is the replacement according the php function preg_replace.
Inspired from middlewares/base-path
Requirements
- PHP >= 7.0
- A PSR-7 http message implementation (Diactoros, Guzzle, Slim, etc...)
- A PSR-15 middleware dispatcher
Installation
This package is installable and autoloadable via Composer as linkeddatacenter/url-rewriter.
composer require linkeddatacenter/url-rewriter
Example
$dispatcher = new Dispatcher([ new Middlewares\BasePath([ [ '/(\w+)' => '/$1/pluto', '/(\w+)/(\w+)/(\w+).(csv|json|xml)(.*)' =>'/$1/docstore?db=$2&table=$3&format=$4$5', ]) ]); $response = $dispatcher->dispatch(new ServerRequest());
Options
__construct(array $rules)
rules := array [ , ,.... ] rule := array [ , ]
Test with docker
docker run --rm -ti -v $PWD/.:/app composer composer install docker run --rm -ti -v $PWD/.:/app composer vendor/bin/phpunit
Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.
The MIT License (MIT). Please see LICENSE for more information.