Symfony2中,嫩枝JSON模板VS json_en code:什么是适合这种情况 [英] Symfony2, Twig JSON templates vs json_encode: what's right for this situation

查看:121
本文介绍了Symfony2中,嫩枝JSON模板VS json_en code:什么是适合这种情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最佳实践快速的问题。

我有一个AJAX应用程序,其中有4对路由使用的jQuery所有返回的JSON。

I have an AJAX application where there are 4 routes that all return JSON to be used by jQuery.

我目前使用这样的事情在控制器

I'm currently using something like this at the end of the Controller

return new Response(json_encode($some_array));

在一个地方,我用JMSSerializer,因为我想要的对象传递出去JavaScript有保障的属性。这似乎从来没有对的,我想知道,在一个点上,如果我要'教'的对象知道如何连接code及其所有属性到JSON和发送出来作为其中的一个方法的返回值。

In one place I use JMSSerializer because the object I'm trying to pass out to Javascript has protected attributes. That never seemed right and I wondered at one point if I should 'teach' the object to know how to encode all its attributes to JSON and send them out as the return value of one of its methods.

无论如何,我今天早上我的做灵机一动是使包含我希望它输出的精确格式一根树枝JSON模板,然后填写从PHP变量。一对夫妇的循环也将是必要的输出对象的集合对象之内。

Anyway I had a brainwave this morning that what I should be doing is to make a Twig JSON template that contains the exact format I want it to output, then fill the variables from the PHP. A couple of loops would also be necessary to output a collection of objects within an object.

这样,有来自究竟它会返回给Javascript PHP的一个明确的承诺/合同,不能搞砸了,如果我碰巧如改变对象。我怀疑这会更容易些谁做的JavaScript来理解,因为模板会更容易阅读比PHP的人。

That way there's a clear promise/contract from the PHP of what exactly it will return to the Javascript, that can't be messed up if I happen to e.g. change the object. I suspect this would be easier for the guy who's doing the javascript to understand since the template would be easier to read than the PHP.

我是在正确的轨道:?我应该使用的 JSON枝条模板,而不是json_en code一般

Am I on the right track: should I be using JSON Twig templates rather than json_encode in general?

修改(同时回到了牧场)

由于张贴这个问题,我的研究说:

Since posting this question my research says:

枝条JSON模板VS json_en code

Twig JSON templates vs json_encode

  • 将消除需要JMSSerializer,而不是非法获得 保护对象的属性。
  • 更多可读/ predictable,特别是对在另一边一个Javascript的人。
  • 在枝杈将检查存在变数会立即标志,如果JSON是不能 符合预期的格式,因为如对象改变。所以使得它 更难破解Javascript和PHP之间的友谊。
  • 在单元测试也将做到这一点(和可以验证值),但可以在以后添加。
  • 分隔外部接口的含义内部对象作为一种模式,我们避免对JMSerializer如何json_en code或要形成JSON任何不必要的细节。使我在什么JSON输出完全控制一个人。
  • 会比json_en code()
  • 在我会避免使用模板来呈现数据作为转义数据等方面的责任,然后在模板说<一href="http://stackoverflow.com/questions/9146460/symfony2-echoing-json-from-a-controller-for-use-in-an-extjs-4-grid">Symfony2:在一个ExtJS 4电网
  • 相呼应的JSON从控制器的使用
  • 表示做{{变量名| json_en code |原}}了很多:请参见 http://twig.sensiolabs.org/ DOC /过滤/ json_en code.html (原指关闭自动转义)
  • 在只JSON数据返回从控制器的响应反正因为symfony并不需要做模板阶段,更高效说<一href="http://stackoverflow.com/questions/8557412/content-length-0-in-a-json-response-symfony2">Content长度:0 JSON响应Symfony2的
  • Would eliminate need for JMSSerializer and not illegally get protected object attributes.
  • More readable/predictable, particularly for a Javascript guy on the other side.
  • Twig will check that variables exist Would immediately flag if the JSON isn't able to match the expected format because e.g. object changed. So makes it harder to break the friendship between Javascript and PHP.
  • Unit test would also do this (and could validate values) but can add that later.
  • Separates internal object from external interface meaning as a pattern, we avoid any unwanted specifics about how json_encode or JMSerializer want to form the JSON. Puts me as a human in complete control of what JSON is output.
  • Will be slower than json_encode()
  • "I would avoid using a template to render the data as the responsibility for escaping data etc is then in the template" says Symfony2: Echoing JSON From a Controller for Use in an ExtJS 4 Grid
  • Means doing {{ variable_name | json_encode | raw }} a lot: see http://twig.sensiolabs.org/doc/filters/json_encode.html (Raw means turn off auto escaping)
  • "It's more efficient to just return the JSON data as the response from the controller anyway as Symfony doesn't need to do the templating stage" says Content length: 0 in a json response Symfony2

推荐答案

如果你担心的是没有合同,那么也许你应该只写一个单元测试。它甚至能走得更远,因为你也可以有一个合同上的属性的值,这样一来。

If what you're worried is the absence of contract, then maybe you should just write a unit test. It could go even further since you could also have a contract on the values of the attributes, this way.

这篇关于Symfony2中,嫩枝JSON模板VS json_en code:什么是适合这种情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆