Symfony2 AJAX implementation sample

So I spent a whole day trying to figure things out.

Basically I had to do several things:

  • create action for the addRecitation
  • create the route with /get/{id}.{_format}
  • return object with a special class Response from XML HTTP request symfony
  • now I am implementing the callback function from ajax javascript the client side

Symfony2 AJAX preparation

As I was going I was advised that the Symfony2 comes with a reverse proxy already built-in which is written in php so it is not the most efficient.
However does the job, and I can do AJAX with it, without or with ESI (provided I enabled it on the config). So I will decide for now not to use ESI enabled, since it is not crucial at this point in time.

I also found out that a best start to work on sf2 and AJAX, is to use jquery which makes it easy to work with.
No need for helpers as we can include the javascript and do call ajax links that tie actions for controllers like:

<a id="ajax_link" href="{{ path('symfony_route') }}">Click this link </a>

Stof’s rule of thumb

apriori:

  • owning side is the side that owns the foreign key
  • inversed side in a many to one relationship is the one side
  • owning side in a many to one relationship is the many side
  • the issue is that the setter of the inversed side needs to update the owning side otherwise you will not be able to use it yourself. And you cannot call the setter of the inversed side from the one of the owning side as it would make an infinite loop

    – Stof