Symfony2 And Single Webpage Applications using a framework like AngularJS
Symfony2 And Single Webpage Applications using a framework like AngularJS
Some details about JSON, Serialization and Models
Do not rebuild any model. Better use FosRestBundle and/or JMS Serializer. It turns the entities with relations into JSON objects. This objects are passed via api to the frontend and work with this just like in twig, During use the angular.js like this
{[{ user.username }]}
is as equal as in twig. But remember to make custom brackets for angular because by default it uses the same as twig.
Routing
The single page application is taken , so symfony’s routing is placed on a low level to have few page refresh. Instead use the routing of the frontend framework.
For example:
app.config(function($routeProvider, $interpolateProvider) {
//here you go, custom brackets $interpolateProvider.startSymbol('{[{'); $interpolateProvider.endSymbol('}]}');
$routeProvider.when('/user', { controller: UserController, templateUrl: Routing.generate('suser_list') }).when('/ticket', { controller: TicketController, templateUrl: Routing.generate('ticket_list') }); });
During hit a link :
<a href="#/ticket">Go to tickets</a>
AngularJs will know which frontend controller to trigger. Pretty great stuff, without page reload. Check the FosJSRoutingBundle. It permit all to make symfony routes in javascript, Use this to link js controllers with html templates where data is pushed in.
FormTypes, Form handling, Validation
During use a frontend framework like angularjs, The symfony form types are not good . Remember data is pushed and pulled via api as json, this would be a hard job for form types to handle this kind of compexity.
For validation use the angular’s live validation or have the symfony’s validation in the backend, no problem. It is good to use both, client and server side validation.
Twig
Twig is not in the race. All data is provided on the client side, and not pre rendered on server side like with twig. But that is only the case if the application is originally a single page application. The twig is only refresh if anyone is reload the entire page.
Integration with Doctrine
Use the doctrine in the backend.
There is no need to rebuild the model in client. Make a service in angularjs that give json data. Data manipulation still happens server side using ajax.
For forms that need csrf , Just send the html rendered by twig via json. Or serialize $form->createView() with jms serializer. However some client script is need to change the json data to actual form controls.