1

Quick question on best practice.

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));

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.

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.

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.

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

EDIT (meanwhile back at the ranch)

Since posting this question my research says:

Twig JSON templates vs json_encode

  • 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
Community
  • 1
  • 1
Adam Knowles
  • 492
  • 5
  • 14
  • Adam - I would suggest changing the 'should' in your question to something more specific. 'Should' depends on who is answering, and is likely to start a debate on a whole load of issues. – Dan Blows Mar 08 '12 at 17:02

1 Answers1

1

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.

greg0ire
  • 22,714
  • 16
  • 72
  • 101
  • That's an excellent suggestion. I've read the Symf2 book on unit testing but not implemented any yet. I can see this would enforce the contract, but my question is what the preferable pattern is. Is it OK to splurge JSON out from a Controller with the format implicit? Are there reasons that's better e.g. speed of execution? Or is an explicit 'this PHP will always produce this JSON' clearer to read and therefore better? – Adam Knowles Mar 08 '12 at 16:04
  • I see... I think you should use a template if you want to separate concerns, if you want the view to be independant from your model. You shoud use `json_encode` if and only if the view should update as soon as the model changes. – greg0ire Mar 08 '12 at 16:50