1

如何在 Zend Expressive 2 (with HtmlResponse) 中更改或添加响应头?

class NotModifiedMiddleware implements ServerMiddlewareInterface
{

    /**
     * Process an incoming server request and return a response, optionally delegating
     * to the next middleware component to create the response.
     *
     * @param ServerRequestInterface $request
     * @param DelegateInterface $delegate
     *
     * @return ResponseInterface
     */
    public function process(ServerRequestInterface $request, DelegateInterface $delegate)
    {

    }
}
4

2 回答 2

1

这简单。

您只需要让委托处理请求并获得响应,例如:

public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
    $response = $delegate->process($request);

    $now = new \DateTime();

    return $response->withHeader('Last-Modified', $now->format('c'));

}
于 2017-09-25T15:26:14.310 回答
1

HtmlResponse,作为第三个参数接收一个在初始化时使用的标头数组。

举个例子:

return new HtmlResponse($data, 200, ['Content-Type' => 'application/x-yaml']);
于 2019-03-21T22:06:54.670 回答