Symfony2 FOSUserBundle如何通过json公开app变量

I have a Symfony 2.3.7 application which uses FOSUserBundle for user management. Till now, the app has been standard MVC with Twig used for templating. Now we are moving toward an Angular based single page application and taking many of the controllers in the original app and converting them to return jsonresponses that the frontend JS will call. Most of this is under way and under control.

My question is on user authentication. Since we need to also replace the login twigs, I'm trying to figure out the most efficient way to manage the user auth. In twig it was very simple:

 {% if app.user %}
     {# display loggedIn user data #}
 {% endif %}

So one way of phrasing the question is ... Is there a way to expose the global app variables (session, user etc) via json? Or is there another way that I haven't thought of yet?

Actually you should not share environment variables between 2 environments. You have many solutions for your login, but you should not use twig.

  • Using oauth (with a bundle ?)
  • Using a more simple token authentication (that means you're gonna create a special security listener for symfony)
  • keep going with the old good system and use a controller to send the login exactly like before

In everycase, sharing environment variable is not a good idea for me since it comes with following problems:

  • Your front depend on your backend
  • Your code is not clear as you can expect because you will not know where comes each variable after some days
  • The Sf environment variables can contains sensible informations.