1
我正在使用FOSRestBundle, 为newRessourceAction
,我想生成一个json版本的表单,我可以使用的模板按顺序排列发布一个新的。fosrestbundle,使用json中的树枝视图_format resultat中的htmlentities编码响应
为此,我一直在测试几种方法来返回一个json响应,但在任何情况下,我的json字符串都会被FOSRestBundle编码。这里有一个例子:
#in the controller
/**
* @View()
*/
public function newFooAction(Request $request) {
$_format = $request->attributes->get('_format');
$foo = new Foo();
$form = $this->createForm(FooType::class, $foo, ['action' => $this->generateUrl('post_foo', ['_format' => $_format]),'method' => 'POST',]);
return ['form' => $form];
}
#in the foo.json.twig
{{ form | serialize('json') }}
输出往往是这样的(注意"
):
{"foo":"bar"}
为了将我的实体序列化为合适的json,我使用了[JMSSerialiserBundle](http://jmsyst.com/bundles/JMSSerializerBundle),这会导致'|序列化( 'JSON')'。如果我使用'| json_encode()',我也遇到了与htmlentities相同的问题。 – Bruno