dpdbu24262 2017-03-16 10:29
浏览 56
已采纳

如何在Symfony中使用FOSUserBundle创建多个登录表单

I use Symfony 2.8.16 with FOSUserBundle 1.3.7 fos user management. In "app/Resources", I have a "FOSUserBundle" folder to surcharge FOSUserBundle template with my template. Currently, I have a login page on the mywebsite.com/login route.

On the other public pages of my website, I want to put a small login form at the top of each page.

How can I do this?

In app/Resources/config/routing.yml

index:
path:     /index
defaults: { _controller: MyBundle:MyController:MyAction }

##################
# FOS User Bundle
##################
fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

In app/Resources/FOSUserBundle/views/security/login.html.twig

{% extends ": MyLayout.html.twig" %}
{% block fos_user_content %}
{% if error %}
    <div class="alert alert-danger">{{ error|trans({}, 'FOSUserBundle') }}</div>
{% endif %}

<form action="{{ path("fos_user_security_check") }}" method="post">
  <input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />
  <div class="row">
    <div class="form-group col-m-6">
      <input type="text" class="_username" id="_username" name="_username" value="{{ last_username }}" placeholder="Email ou identifiant" required="required" />
    </div>
    <div class="form-group col-m-6">
      <input type="password" class="_password" id="_password" name="_password" placeholder="Mot de passe" required="required" />
    </div>
  </div>

  <div class="form_action">
    <button type="submit" class="button button-default-outline" id="_submit" name="_submit" value="{{ 'security.login.submit'|trans({}, 'FOSUserBundle') }}">Connexion</button>
    <div class="__sub">
      <label class="remember">
        <input type="checkbox" id="remember_me" name="_remember_me" checked> {{ 'security.login.remember_me'|trans({}, 'FOSUserBundle') }}
        <span class="separator">|</span>
        <a href="#" class="forgot">Mot de passe oublié ?</a>
      </label>
    </div>
  </div>

</form>
{% endblock fos_user_content %}
  • 写回答

1条回答 默认 最新

  • dongnai8013 2017-03-16 10:51
    关注

    You can add a login form on your layout that submits in the fos login check :

        <form action="{{ path('fos_user_security_check') }}" method="post">
            <input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}">
            <input type="text" name="_username" />
            <input type="password" name="_password" />
            <input type="checkbox" name="_remember_me" checked />
            <input type="submit" value="Submit" />
        </form>
    

    You can, of course, change the style, add info and labels but this inputs must be there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 u盘问题:盘符不显示 无媒体
  • ¥50 R语言读取nc按月均值转为tif
  • ¥30 智能车串级pid调参
  • ¥15 visual studio code翻译老是错误
  • ¥20 卫星测高数据的高程转换
  • ¥15 爬取招聘网站数据信息
  • ¥15 安装完tensorflow,import tensorflow as tf后报错,如何解决?
  • ¥15 ultralytics库导出onnx模型,模型失去预测能力
  • ¥15 linux下点对点协议连接2个USB串口的硬件流量控制问题
  • ¥15 SQL数据自动生成问题