middlewares / base-path
Middleware to remove the prefix from the uri path of the request.
Installs: 1 036 985
Dependents: 8
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.2 || ^8.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- laminas/laminas-diactoros: ^2.3
- middlewares/utils: ^3.0
- oscarotero/php-cs-fixer-config: ^1.0
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^8|^9
- squizlabs/php_codesniffer: ^3.0
README
Middleware to remove the prefix from the uri path of the request. This is useful to combine with routers if the root of the website is in a subdirectory. For example, if the root of your website is /web/public
, a request with the uri /web/public/post/34
will be converted to /post/34
.
Requirements
- PHP >= 7.2
- 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 middlewares/base-path.
composer require middlewares/base-path
Usage
Set the prefix to remove:
Dispatcher::run([ new Middlewares\BasePath('/base/path') ]);
fixLocation
Used to add the prefix to the Location
header (for redirects). For example:
$response = Dispatcher::run([ (new Middlewares\BasePath('/base/path'))->fixLocation(), function () { return Factory::createResponse(301)->withHeader('Location', '/post/1'); } ]); echo $response->getHeader('Location'); // Returns /base/path/post/1
attribute
This method will store the original path in an attribute.
// Save the original uri with basepath in the custom attribute "before-basepath-uri" $basepath = (new Middlewares\BasePath())->attribute('pre-basepath-path');
Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.
The MIT License (MIT). Please see LICENSE for more information.