slam / basepath
PHP 中间件,用于从请求 URI 中移除路径前缀
v3.0.0
2021-03-09 14:39 UTC
Requires
- php: ^7.4 || ^8.0
- psr/container: ^1.1.1
- psr/http-message: ^1.0.1
- psr/http-server-handler: ^1.0.1
- psr/http-server-middleware: ^1.0.1
Requires (Dev)
- ext-json: *
- laminas/laminas-coding-standard: ^2.1.4
- laminas/laminas-diactoros: ^2.5.0
- laminas/laminas-view: ^2.12.0
- malukenho/mcbumpface: ^1.1.5
- mezzio/mezzio-fastroute: ^3.1.0
- mezzio/mezzio-helpers: ^5.4.0
- mezzio/mezzio-laminasviewrenderer: ^2.2.0
- mezzio/mezzio-router: ^3.3.0
- phpstan/phpstan: ^0.12.81
- phpstan/phpstan-phpunit: ^0.12.18
- phpunit/phpunit: ^9.5.2
- squizlabs/php_codesniffer: ^3.5.8
This package is auto-updated.
Last update: 2024-09-09 22:28:35 UTC
README
此中间件仅从请求 URI 中移除前缀。
安装
此中间件可以使用 composer 安装。
$ composer require slam/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 ... ];