samsonasik/expressive-redirect-handler

用于URL重定向处理的Expressive中间件

2.0.0 2020-01-08 17:46 UTC

README

PHP version Software License Build Status Coverage Status Downloads

这是版本^2.0的README,该版本仅支持Mezzio版本3和php^7.1。

对于版本^1.0,您可以在此阅读版本1.* README,它支持ZF Expressive版本3和php^7.1。

对于版本0.*,您可以在此阅读版本0.* README,它仍支持ZF Expressive版本1和2,以及php^5.6|^7.0。

ExpressiveRedirectHandler是一个包含Mezzio中间件的包,用于处理符合以下条件的重定向,以匹配mezzio-skeleton

  1. 当给定的URL未在路由配置中注册到RedirectResponse

例如,我们在中间件中使用RedirectResponse实例

use Laminas\Diactoros\Response\RedirectResponse;
// ...
$redirect = '/foo'; // may be a variable from GET
return new RedirectResponse($redirect);

如果传递的$redirect作为URL在路由中有效且已注册,它将使用默认的RedirectResponse实现,否则,它将重定向到在config/autoload/expressive-redirect-handler.local.php中注册的默认default_url

例如,我们定义

<?php

return [

    'expressive-redirect-handler' => [
        'allow_not_routed_url' => false,
        'default_url' => '/',

        'options' => [
            'exclude_urls' => [
                // 'https://www.github.com/samsonasik/ExpressiveRedirectHandler',
            ], // to allow excluded urls to always be redirected
            'exclude_hosts' => [
                // 'www.github.com'
            ],
        ],
    ],

    // ...
];

这意味着,我们不能允许重定向到未注册的路由之外,无论何时在路由中找到未注册的URL,我们都会被重定向到默认_url。它还禁用了重定向到自身,因此您不能重定向到自身。

对于某些特殊的URL(即使未在路由中注册,也可以重定向),您可以在exclude_urls/exclude_hosts选项中注册。

如果您定义了exclude_urls/exclude_hosts选项,其中一个是您自己的当前URL/主机/域名,那么您仍然有风险会得到“无限”的重定向循环。因此,请确保exclude_urls/exclude_hosts不是您的当前自身。

  1. 当您想根据HTTP状态码重定向到特定URL时
<?php
return [

    'expressive-redirect-handler' => [
        'allow_not_routed_url' => false,
        'default_url' => '/',
        'header_handler' => [
            'enable' => true, // enable it!
            'headers' => [
                401 => '/login',
                503 => '/maintenance',
            ],
        ],
    ],
    // ...
];

基于上述配置,当HTTP状态码为401或503时,它将重定向到其配对的值。

安装

  • 通过Composer要求
composer require samsonasik/expressive-redirect-handler
  • vendor/samsonasik/expressive-redirect-handler/config/expressive-redirect-handler.local.php.dist复制到config/autoload/expressive-redirect-handler.local.php并根据需要修改。

  • 打开config/pipeline.php并添加

$app->pipe(ExpressiveRedirectHandler\Middleware\RedirectHandlerAction::class);

在非常第一条管道记录。

贡献

贡献非常受欢迎。请阅读CONTRIBUTING.md

致谢