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

时间:2017-09-13 06:50:53

标签: php request response zend-framework3 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)
    {

    }
}

2 个答案:

答案 0 :(得分:1)

这很容易。

您只需让代理处理请求并获得回复,例如:

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

    $now = new \DateTime();

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

}

答案 1 :(得分:1)

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

例如:

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