在symfony2中将额外的新参数作为响应添加到标头

在symfony2中将额外的新参数作为响应添加到标头,symfony,header,response,Symfony,Header,Response,我想向客户端发送响应,该响应应该在标题中包含一些常见的细节,比如说userID和正文中的其他数据。如何将这些新参数添加到响应头中 我试过了 public function postAPIAction() { $jsonData=$this->getRequest()->getContent(); $decodepostrest=json_decode($jsonData,true); //涉及处理。。。。。。。。 $uniqueKey=$this->generateUniqueKey(); $r

我想向客户端发送响应,该响应应该在标题中包含一些常见的细节,比如说
userID
和正文中的其他数据。如何将这些新参数添加到响应头中

我试过了

public function postAPIAction()
{
$jsonData=$this->getRequest()->getContent();
$decodepostrest=json_decode($jsonData,true);
//涉及处理。。。。。。。。
$uniqueKey=$this->generateUniqueKey();
$response=新响应();
$response->headers->add(数组('userId'=>$uniqueKey));
返回新的响应(json_encode(数组('errorcode'=>'1'),true));
}

这不起作用。

您必须返回已设置标题的响应,而不是在
return
语句中创建一个新的响应。

您正在返回中创建一个新的响应。 您应该使用之前创建的响应

$response = new Response();
$response->headers->add(array('userId' => $uniqueKey));
$response->setContent(...);

return $response;

这应该行得通。请从您的控制器粘贴更多代码ID您
返回操作中创建的请求?好的..谢谢..我出错..我返回了新的响应。谢谢在评论中保留答案不好,因此我提供了您可以检查为正确答案的答案。