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条)

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算