middlewares / base-path
中间件,用于从请求的uri路径中移除前缀。
v2.1.0
2021-04-08 19:21 UTC
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
中间件,用于从请求的uri路径中移除前缀。当网站的根目录位于子目录中时,与路由器结合使用非常有用。例如,如果您的网站根目录为 /web/public
,则uri为 /web/public/post/34
的请求将被转换为 /post/34
。
需求
安装
此包可通过Composer安装和自动加载,包名为 middlewares/base-path。
composer require middlewares/base-path
使用方法
设置要移除的前缀
Dispatcher::run([ new Middlewares\BasePath('/base/path') ]);
fixLocation
用于向Location
头部添加前缀(用于重定向)。例如
$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
属性
此方法将原始路径存储在属性中。
// Save the original uri with basepath in the custom attribute "before-basepath-uri" $basepath = (new Middlewares\BasePath())->attribute('pre-basepath-path');
有关最近更改的更多信息,请参阅CHANGELOG,有关贡献细节,请参阅CONTRIBUTING。
MIT许可证(MIT)。有关更多信息,请参阅LICENSE。