donglvchu9143 2012-06-28 20:19
浏览 75
已采纳

:: base.html.twig中不存在变量“csrf_token”

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?

  • 写回答

2条回答 默认 最新

  • duan2428 2012-06-28 20:48
    关注

    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_token variable 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来