2

So I've been tasked to build an API and a frontend for a project. The only real requirement that I've been given is that I have to use PHP to build the API.

I developed in raw PHP once, and it was a small two month personal project, and it was seven years ago. So I looked at the frameworks available and went ahead with Symfony.

On the front end I was hoping to move ahead with Angular.

I've got the API mostly built out, but my problem now is serving the angular files inside of the Symfony process. It seems that for every path I need I'll need to write an action in the controller for that route, which I'm for the most part ok with. But the big problem that I just ran into is that it's always assuming I want Twig as my engine. So I'm not able to use the braces that Angular uses.

Is this at all possible or have I gotten myself tied in to this incorrectly?

EDIT: For now I'm going to move ahead with wrapping my angular in the verbatim tag

{% verbatim %}
  {{ angular code }}
{% endverbatim %}
ELepolt
  • 373
  • 2
  • 4
  • 16

4 Answers4

3

You said :

It seems that for every path I need I'll need to write an action in the controller for that route

but angular2 is used for Single Page Application (SPA), so you need only one page to serve it: 1 route > 1 action > 1 twig template.

For this template, you can change the start and end interpolation tags: https://docs.angularjs.org/api/ng/provider/$interpolateProvider

Or use verbatim twig keyword to let interpolate characters for angular: http://twig.sensiolabs.org/doc/tags/verbatim.html

But for your angular2 templates, you don't have to use twig, no need to mix two templating languages. Your API gives data to angular which reflects it inside templates.

And an other good practice is to precompile all your angular templates inside $templateCache using things like that: https://www.npmjs.com/package/gulp-ng-template

bertrandg
  • 3,147
  • 2
  • 27
  • 35
  • The path explanation was poor. For every .js file that is referenced I need to create a route for it. Right now I'm just pointing at a cdn to make that easier but I still have to create a route for my personal app.js and such. – ELepolt Jan 22 '16 at 15:37
  • A good practice is to use a tool like Gulp/Grunt to concate (and a lot of others things) all js files in one. You can place it inside the `web` folder so it's accessible and you don't need to create a symfony route. Then simply add a `` and it's done – bertrandg Jan 23 '16 at 21:40
  • 1
    In fact if symfony is just used to give a REST api, you can totally dissociate your angular application from it. You can place it inside a different folder or even an other server. – bertrandg Jan 23 '16 at 21:47
2

i have also worked in symfony and angular2

how do i started

don't merge two projects as we can do it for angular 1.4 version.

as angular2 is frontend and it support cli .

symfony is backend so try to use as service

i would suggest you should use symfony2 as service

where you can you do add/edit/delete and create frontend routing.

i have made demo angular2 with symfony2

Github url

https://github.com/afeef1915/Angular2

if finally you

integrate angular2 with symfony then would have face routing problem as symfony2 would not understand angular2 routing.

and twig syntax much similar to angular component.html files

afeef
  • 4,396
  • 11
  • 35
  • 65
1

I would prefer to develop the frontend and API independently. That would be the point of an API for me.

It seems that for every path I need I'll need to write an action in the controller for that route, which I'm for the most part ok with.

-> really worse.

For the twig angular syntax conflict, you can change the start and end interpolation tags using interpolateProvider service:

angular.module('myApp', []).config(function($interpolateProvider){
    $interpolateProvider.startSymbol('{[{').endSymbol('}]}');
});

see the docs.

Tino Rüb
  • 799
  • 2
  • 13
  • 27
  • 2
    Oops, the question seems to be more common... For example, see [here](http://stackoverflow.com/questions/13671701/angularjs-twig-conflict-with-double-curly-braces) for a more detailed answer. – Tino Rüb Jan 21 '16 at 22:50
1

We are moving the UI of a large Symfony app to Angular as well, and have decided to use API Platform (https://api-platform.com/). Previously, we used FOSRestBundle, but are dropping that for this new version.

So far we've been pleased with this approach.

You asked about serving Angular files from within Symfony. We opted to keep them in a separate repo, so they could even be deployed on a different server. That would be my recommendation, although of course you could put the Angular files in the /web directory. Doing that, though, complicates deployment.

Tac Tacelosky
  • 3,165
  • 3
  • 27
  • 28
  • So I believe this is the "correct" way to go about this, but for larger applications that can't afford a "rewrite" to get to that level, I feel that they need to be shoved together for the interim, though I know that creates a lot of re-work later, but no time for re-write always introduces things. I'm looking at using Api-Platform as well. We have FOSRest, but aren't using it much if at all. – Jarvis Dec 27 '16 at 17:12