-2

I have very basic Symfony2 application. I would like including AngularJS within it.

  1. Where should I put my Angular resources?
  2. How to include them?
  3. How to communicate between Angular and Symfony

I see that official documentation says that I should split frontend and backend so that there cannot be any conflict.

Any scaffolding examples?

miksiii
  • 2,426
  • 26
  • 22

2 Answers2

3

Well, Symfony2 is a backend framework, AngularJS a frontend framework. I would recommend building a REST API and have it return JSON. In AngularJS, you can use $resource to interact with the RESTful service.

1

As Angular resources must be directly accessible from the web browser in their entirety, you should put all these files within the [Symfony app root]/web/ directory. When an HTTP request requests a file located there, it will be served without interacting with the rest of the framework.

Optionally (and preferably) these pages can be pointed to with the routing component to make pretty URLs.

I agree with user3142446's answer about creating a REST API and allowing Angular to perform all of the heavy work. There's a well documented bundle for creating REST API's with Symfony called FOSRestBundle to get you started. By using this strategy you can utilize anything done in Symfony the same way you use external APIs.

Community
  • 1
  • 1
HPierce
  • 7,249
  • 7
  • 33
  • 49
  • Hi @HPierce and thanks for your feedback. What about views? Since both AngularJS and Symfony have their own views should I ignore *.twigs and build whole frontend with angular (define views, templates, routing, etc)? Who is going to serv the views? – miksiii Nov 02 '15 at 20:21
  • @miksiii, Yep. I think that's a better solution. It should allow you to treat anything you do with Symfony like you would any other API. – HPierce Nov 02 '15 at 20:26
  • where should I put angular views? where to put main view that declare an angular app? should I rename extensions from base.html.twig to base.html, etc. How app structure should look like? Thanks – miksiii Nov 02 '15 at 20:51
  • and most importantly how to call symfony controller from angular? For example how to do make a post request to specific controller. – miksiii Nov 02 '15 at 22:47
  • I don't know about symfony, but to get started with AngularJS views and such, just visit the [AngularJS tutorial page](https://docs.angularjs.org/tutorial). Everything you need to know to get started is fairly well documented there. –  Nov 03 '15 at 17:31
  • @Ben de Laporte I know Angular very well but got confused when putting all together with symfony2. I solved my problems anyway. Now I ran in new Errors and Exceptions which are good. Thanks again guys! – miksiii Nov 03 '15 at 23:14