middlewares/base-path

中间件,用于从请求的uri路径中移除前缀。

v2.1.0 2021-04-08 19:21 UTC

This package is auto-updated.

Last update: 2024-08-29 03:23:58 UTC


README

Latest Version on Packagist Software License Testing Total Downloads

中间件,用于从请求的uri路径中移除前缀。当网站的根目录位于子目录中时,与路由器结合使用非常有用。例如,如果您的网站根目录为 /web/public,则uri为 /web/public/post/34 的请求将被转换为 /post/34

需求

  • PHP >= 7.2
  • PSR-7 http消息实现(例如DiactorosGuzzleSlim等)
  • PSR-15中间件分发器

安装

此包可通过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