Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7607765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:44:53+00:00 2026-05-31T00:44:53+00:00

Quick question on best practice. I have an AJAX application where there are 4

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:44:55+00:00Added an answer on May 31, 2026 at 12:44 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a quick question - is there a best practice in SQL Table
Just a quick question about best practice in MVC development. Let's say that I've
I just have a quick best practice question regarding custom cells in a UITableView.
I have a quick question on best practice and if getters and setters are
A quick question about best practice with PHP classes. I have seen people use
Quick question... I have a query that checks for duplicates that looks like this:
Quick question. I have an app that use a native DLL through PInvoke, this
Have a quick question about what would be the best way to implement iterators
I have read the following question Javascript and Anchor Tags, Best Practice? And it
Quick question just to verify something. If I have an HTML5 mobile advertisement that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.