如何在 Zend Expressive 中更改或添加 header

How to change or add a header in Zend Expressive

如何更改或添加 header 到 Zend Expressive 2 中的响应(使用 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)
    {

    }
}

很简单。

你只需要让委托来处理请求并得到响应,例如:

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

    $now = new \DateTime();

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

}

HtmlResponse,接收作为第三个参数的 headers 数组,用于初始化。

举个例子:

return new HtmlResponse($data, 200, ['Content-Type' => 'application/x-yaml']);