简体   繁体   English

在zf2中设置标题

[英]Set headers in zf2

i have set up a following headers in my controller, with the following code 我在控制器中设置了以下标头,其中包含以下代码

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

how can i achieve this in the zend framework 2, 我如何在zend框架2中实现这一目标,

Thanks 谢谢

use Zend\Http\Headers;

... ...

$headers = new Headers();

or 要么

$headers = $httpObject->getHeaders();

then to add headers one by one 然后一一添加标题

$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');

or pass all in one array like this 或像这样通过一个数组

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

if you created a new headers object 如果创建了新的标头对象

$httpObject->setHeaders($headers);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM