Symfony2 and FOS User Bundle issue…
I have implemented my own login form in the head of the page (using FOS User Bundle as per directions in http://forum.symfony-project.org/viewtopic.php?f=31&t=37767.)
Now, I have the error message
Variable "csrf_token" does not exist in ::base.html.twig
Someone else on that forum (url above) had the exact same issue, and resolved it by putting in the hidden csrf field like this
<input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />
My problem though is: that works fine when on the /login path, but my whole point is not needing that separate login page, I want my form integrated in the page head always, and it seems that the {{csrf_token}} is only available when the page is rendered by the login controller…
Also, how do I get the error messages (flashes) in there, I assume I must include the login controller in my defaultController somehow, to get all those variables with it every time, or…? Or should I instead render the userBundle controller in that portion of the header instead of pasting some of the userBundle template into my head?
If you create your own login form using the Form component then you should already have a csrf token as a hidden field. The reason that the
csrf_tokenvariable is available only on /login is because the default FOSUserBundle login controller doesn’t use a form, instead they just generate a csrf token manually as seen here and pass it to the view.So, either make sure you are rendering all the hidden fields on your form with something like
{{ form_rest(form) }}or generate a csrf_token manually like the FOSUserBundle does here and render it with the code you already have.