slam/basepath

PHP 中间件,用于从请求 URI 中移除路径前缀

v3.0.0 2021-03-09 14:39 UTC

This package is auto-updated.

Last update: 2024-09-09 22:28:35 UTC


README

Latest Stable Version Downloads Integrate

此中间件仅从请求 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 ...
];