Routing problems with AngularJS and Symfony

868 Views Asked by At

I'm trying to work with symfony2 and AngularJs together, but I cant,

  1. I've conflicts with the rounting
  2. Angular does not recognize the Angular symbols in twig templates.

This is my app.js that is in AppBundle/Resoures/public/js/app.js

var app = angular.module('uifi',[]);

app.config(['$stateProvider','$interpolateProvider',
  function($stateProvider,$interpolateProvider){
    $interpolateProvider.startSymbol('[[').endSymbol(']]');

    $stateProvider.state('grupos', {
        url: '/grupos',
        templateUrl: Routing.generate('app_grupos_index'),
        controller : 'GruposController'
    });
}]);

This is my page index.html.twig that has a dummy content

<html ng-app="uifi">

<body>

    <script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
    <script src="{{ path('fos_js_routing_js', {"callback": "fos.Router.setData"}) }}"></script>

    <script src="{{ asset('js/angular.min.js') }}"></script>
    <script src="{{ asset('bundles/app/js/app.js') }}"></script>
    <script src="{{ asset('bundles/app/js/controllers/GrupoController.js') }}"></script>

    <div ng-controller="GruposController">
        [[title]]
    </div>
</body>
</html>

And this is my Angular controller to setup the title variable

app.controller('GruposController',['$scope',
  function($scope){
      $scope.title ='Research groups';
  }
]);

Then my controller in symfony to handle the app_grupos_index

class GruposController extends Controller{

  /**
   * @Route(name="app_list_grupos",options={"expose"=true})
   */
  public function getListGrupos(){
    $grupos = $this->get('uifi.integrantes.grupos')->getGrupos();
    return new JsonResponse( array('grupos'=> $grupos) );
  }

  /**
   * @Route(name="app_grupos_index",options={"expose"=true})
   */
  public function indexAction(){
    return  $this->render('AppBundle:Grupos:index.html.twig');
  }
}

Note that I'm using FOSJsRoutingBundle to expose the symfony routes to Javascript

Problems:

  1. If I go to the route http://localhost:8000/grupos in the browser I get the following Symfony error:

    No route found for "GET /grupos"

    This problem make me think in that Angular is not working with the routing

  2. If I modify the Symfony controller to handle the route /grupos like this:

  /**
   * @Route("/grupos",name="app_grupos_index",options={"expose"=true})
   */
  public function indexAction(){
    return  $this->render('AppBundle:Grupos:index.html.twig');
  }

I get my page with the explicit [[title]] string and the following Angular error:

Error: [$injector:modulerr] http://errors.angularjs.org/1.3.14/$injector
/modulerr?p0=uifi&p1=%5B%24injector%3Amodulerr%5D%20http%3A%2F
%2Ferrors.angularjs.org%2F1.3.14%2F%24injector%2Fmodulerr%3Fp0%3DngRoute%26p1....

So, I'm wondering what am I doing wrong?

1

There are 1 best solutions below

0
On

There are lot more things you'll need to consider when integrating symfony with angular.

  1. Routing should completely handled in client side. Symfony should only render the initial index route. There is no point of having angular here if you'd use server side rendering.
  2. Backend should act as an API provider, and frontend should act accordingly
  3. Authentication, Authorization - couple of possibilities at least

    • WSSE - stateless, token based
    • cookie based - You'll need to have custom authentication handlers and logout success handlers to send JSON responses
  4. Use verbatim tags than customizing interpolation symbols in angular - it'll likely to break third party plugins or directives.

Two examples

  1. https://github.com/thujeevan/sad - uses cookie based authentication
  2. https://github.com/FlyersWeb/angular-symfony - uses WSSE based authentication

Hope, you'll catch things up