在zf2中设置标头

时间:2015-02-17 12:51:16

标签: php rest zend-framework2 http-headers httprequest

我在控制器中设置了以下标题,代码如下

header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST');
    header("Access-Control-Allow-Headers: X-Requested-With, Content-Type");

我如何在zend框架2中实现这一目标,

由于

1 个答案:

答案 0 :(得分:2)

use Zend\Http\Headers;

...

$headers = new Headers();

$headers = $httpObject->getHeaders();

然后逐个添加标题

$headers->addHeaderLine('Access-Control-Allow-Origin', '*');
$headers->addHeaderLine('Access-Control-Allow-Methods', 'GET, POST');
$headers->addHeaderLine('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');

或传递所有像这样的数组

$headers->addHeaders(array(
    'Access-Control-Allow-Origin' => '*',
    'Access-Control-Allow-Methods' => 'GET, POST',
    'Access-Control-Allow-Headers' => 'X-Requested-With, Content-Type'
));

如果您创建了新的标题对象

$httpObject->setHeaders($headers);