I have created middleware and implement the following code but not working. Why ?
public function addHeaders(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
if ($request->getHeader('Origin')) {
$response = $response
->withHeader('Access-Control-Allow-Origin', $this->_allowOrigin($request))
->withHeader('Access-Control-Allow-Credentials', $this->_allowCredentials())
->withHeader('Access-Control-Max-Age', $this->_maxAge());
if (strtoupper($request->getMethod()) === 'OPTIONS') {
$response = $response
->withHeader('Access-Control-Expose-Headers', $this->_exposeHeaders())
->withHeader('Access-Control-Allow-Headers', $this->_allowHeaders($request))
->withHeader('Access-Control-Allow-Methods', $this->_allowMethods());
}
}
return $response;
}