kovagoz/http-middleware-request-forwarder

PSR-15 中间件栈的内部重定向机制

dev-master 2022-07-28 07:21 UTC

This package is auto-updated.

Last update: 2024-09-28 12:00:40 UTC


README

这个库的目标是使得将HTTP请求传递到另一个处理器成为可能,从而创建所谓的内部重定向。

phpunit workflow

要求

  • PHP >= 8.0

用法

在请求处理器之前将此中间件放入堆栈中。如果您想将请求传递到另一个处理器,则从当前处理器返回带有 X-Internal-Redirect 标头的响应,其值应该是目标处理器的名称。

如果您使用 PSR-17 工厂,代码将如下所示

<?php

class SomeHandler implements RequestHandlerInterface
{
    public function handle(ServerRequestInterface $request)
    {
        return $this->responseFactory
            ->createResponse()
            ->withHeader('X-Internal-Redirect', AnotherHandler::class);
    }
}

如果您正在使用 kovagoz/http-responder 库,那么这个包中找到的 HttpResponder 扩展可以简化转发过程。

<?php

// Extend the HttpResponder...
$responder = new class($responseFactory, $streamFactory) extends HttpResponder {
    use ResponseHelper;
};

// ...then call forward() in your handler.
return $responder->forward(AnotherHandler::class);