Symfony2,Twig JSON模板与json_encode:在这种情况下的正确选择 [英] Symfony2, Twig JSON templates vs json_encode: what's right for this situation

查看:104
本文介绍了Symfony2,Twig JSON模板与json_encode:在这种情况下的正确选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关最佳做法的简短问题.

Quick question on best practice.

我有一个AJAX应用程序,其中有4条路由全部返回JSON以供jQuery使用.

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的对象具有受保护的属性.这似乎从来都不对,我想知道是否应该教一个对象以知道如何将其所有属性编码为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.

无论如何,今天早上我总是脑子里想,我应该要做的是制作一个Twig 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.

这样一来,PHP就明确返回了将要返回Javascript的明确承诺/合同,如果我碰巧会碰到这个问题,例如,更改对象.我怀疑这样做对使用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 Twig模板,而不是一般的json_encode吗?

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:

Twig JSON模板与json_encode

Twig JSON templates vs json_encode

  • 将消除对JMSSerializer的需求,并且不会非法获取 受保护的对象属性.
  • 更具可读性/可预测性,尤其是对于另一端的Java专家而言.
  • Twig将检查变量是否存在,如果JSON无法执行,将立即进行标记 符合预期格式,例如对象已更改.所以做到了 更难以打破Javascript和PHP之间的友谊.
  • 单元测试也可以做到这一点(并可以验证值),但可以稍后添加.
  • 将内部对象与外部接口分离为一种模式,我们避免了有关json_encode或JMSerializer如何形成JSON的任何不必要的细节.让我作为一个人可以完全控制输出什么JSON.
  • 会比json_encode()慢
  • 我会避免使用模板来呈现数据,因为转义数据的责任就在模板中", http://twig.sensiolabs.org/doc/filters/json_encode.html (原始表示关闭自动转义)
  • 无论如何,由于Symfony不需要执行模板化阶段,仅返回JSON数据作为来自控制器的响应,效率更高."
  • 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,Twig JSON模板与json_encode:在这种情况下的正确选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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