los / basepath
PHP 中间件,用于从请求 URI 中移除路径前缀
2.2.0
2021-04-13 11:56 UTC
Requires
- php: ^7.4 || ^8.0
- mezzio/mezzio-helpers: ^5.4
- psr/container: ^1.1
- psr/http-message: ^1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- ext-json: *
- doctrine/coding-standard: ^9.0
- laminas/laminas-diactoros: ^2.5
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.5
- vimeo/psalm: ^4.7
README
此中间件仅从请求 URI 中移除前缀。
安装
可以使用 composer 安装此中间件。
$ composer require los/basepath
用法
只需将中间件添加到您应用程序中的第一个中间件之一。
例如
$app->pipe(new \LosMiddleware\BasePath\BasePathMiddleware('/site'));
具有 /site
前缀的每个请求都将被替换
/site => /
/site/blog => /blog
/site/contact-us => /contact-us
Mezzio(以前称为 Zend Expressive)
如果您使用 mezzio-skeleton,可以将 config/los-basepath.global.php.dist
复制到 config/autoload/los-basepath.global.php
并根据您的需求修改配置。
然后,将中间件添加到您的流水线
$app->pipe(LosMiddleware\BasePath\BasePathMiddleware::class);
动态基本路径
在某些情况下,可能需要动态基本路径。您可以在配置文件中使用以下代码实现此功能
$scriptPath = dirname($_SERVER['SCRIPT_NAME']); return [ // Use directory of script path if available, otherwise default to empty string. 'los' => [ 'basepath' => strlen($scriptPath) > 1 ? $scriptPath : '', ], // rest of the configuration ... ];