[英]How to change or add a header in Zend Expressive
How change or add a header to the response in Zend Expressive 2 (with HtmlResponse) ? 如何在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)
{
}
}
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'));
}
HtmlResponse , recieves as a third param an array of headers to use at initialization. HtmlResponse ,将在初始化时使用的标头数组作为第三个参数。
As an example: 举个例子:
return new HtmlResponse($data, 200, ['Content-Type' => 'application/x-yaml']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.