【问题标题】:Symfony2: Passing "_format=xml" does not render responce in XMLSymfony2:传递“format=xml”不会在 XML 中呈现响应
【发布时间】:2015-02-14 11:43:49
【问题描述】:

将 defaults={"_format" = "xml"} 传递给控制器​​的奇怪行为(之前似乎有效):

/**
 * @Route("/orderxml/{orderguid}", name="_show_order_xml", defaults={"_format" = "xml"})
 */
public function showOrderXML($orderguid)
{
....
$xmloutput = $this->container->get('templating')
        ->render($templateName, $tpl_data);
$response = new Response($xmloutput);
}

虽然我通过了 defaults={"_format" = "xml"},但仍会收到 content-type=text/html 的响应。

尝试调试请求 - 它带有空的 Content-Type 标头。 Request的属性确实包含_format=xml,但也包含media-type="text/html",我不熟悉。如文档中所述,_format 确定 Request 和 Response 对象的内容类型。

目前我唯一能做的就是 $response->headers->set('Content-Type', 'text/xml');

如何解决这个问题?

PS:symfony 2.3

【问题讨论】:

  • 您在哪里读到 _format 适用于响应?很确定它只适用于请求。手动设置标题是最简单的选择。或者,您可以使用 JsonResponse 作为指南创建一个 XmlResponse 对象。

标签: symfony


【解决方案1】:

_format 定义内容类型,但您仅将其设置为默认值。由于请求使用 contant-type html,因此默认值无关紧要。您还必须将需求中的 _format 也设置为 xml。

【讨论】:

  • 是的,这就是问题所在。顺便说一句,如果我的请求是使用 content-type=application/json 发送的,我假设它会返回 json 并且不需要设置要求?
  • 是的。但是要求很好接受,例如只有 json 和 xml
猜你喜欢
  • 1970-01-01
  • 2019-12-15
  • 1970-01-01
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多