How to change or add a header in Zend Expressive

674 views Asked by At

How change or add a header to the response in 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)
    {

    }
}
2

There are 2 answers

0
Azucena H On BEST ANSWER

HtmlResponse, recieves as a third param an array of headers to use at initialization.

As an example:

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

It's easy.

You just need to let the delegate to process the request and get response back, for example:

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

    $now = new \DateTime();

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

}