我想在我RandomController::indexAction()的XML响应中返回:
return new Response($this->renderView(
'AcmeRandomBundle:Random:index.xml.twig',
array(
'randomParameter' => $randomParameter
)
));
Run Code Online (Sandbox Code Playgroud)
那样的地方index.xml.twig:
<?xml version="1.0" encoding="UTF-8"?>
<randomTag>
{{ randomParameter }}
</randomTag>
Run Code Online (Sandbox Code Playgroud)
当我想在firefox中打开这个动作时,我会进入firebug:
<html>
<body>
<randomTag>
randomValue
</randomTag>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
如何返回正确的XML响应?
Mat*_*teo 41
尝试在响应对象上添加正确的标头,如:
$response->headers->set('Content-Type', 'text/xml');
Run Code Online (Sandbox Code Playgroud)
否则defaults,在Controller方法中添加正确的注释(),如下例所示:
/**
* @Route("/hello/{name}", defaults={"_format"="xml"}, name="_demo_hello")
* @Template()
*/
public function helloAction($name)
{
return array('name' => $name);
}
Run Code Online (Sandbox Code Playgroud)
Ala*_*blo 11
如果您有许多 XmlResponse 要返回,请考虑创建您自己的 Response 对象:
<?php
namespace App\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\Response;
class XmlResponse extends Response
{
public function __construct(?string $content = '', int $status = 200, array $headers = [])
{
parent::__construct($content, $status, array_merge($headers, [
'Content-Type' => 'text/xml',
]));
}
}
Run Code Online (Sandbox Code Playgroud)
然后您可以返回new XmlResponse($xmlBody);控制器。
| 归档时间: |
|
| 查看次数: |
16520 次 |
| 最近记录: |