在Symfony 2中发送纯文本标题


Sending text plain header in Symfony 2

我有一个操作可以让客户预览系统电子邮件,我想为电子邮件的纯文本版本发送text/plain标头。

我试着遵循Symfony文档:Symfony部分的请求和响应。然而,无论我做什么,我的控制器都会发送text/html内容类型。

这是我的行动:

function showAction($action = null, $format = null){
   $locale = $this->get('session')->getLocale();
   $format = $this->getRequest()->get("format");
   $format = isset($format) ? $format : 'html';

   if ($format === 'text'){
       $response = new Response();
       $response->headers->set('Content-Type', 'text/plain');
       $response->sendHeaders();
   }
   $view = sprintf('MyBundle:Email:%s.%s.%s.twig', 
         $action,$locale,$format);
   return $this->render($view, array());
}

那么,我该如何发送纯文本标题,哪里出错了呢?

您需要添加$response来呈现调用

return $this->render($view, array(), $response);