繁体   English   中英

如何在Zend Expressive中更改或添加标题

[英]How to change or add a header in Zend Expressive

如何在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 ,将在初始化时使用的标头数组作为第三个参数。

举个例子:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM