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 %}

图片转代码服务由CSDN问答提供 功能建议

我使用Symfony 2.8.16和FOSUserBundle 1.3.7 fos用户管理。 在“app / Resources”中,我 有一个“FOSUserBundle”文件夹,用我的模板来收取FOSUserBundle模板。 目前,我在mywebsite.com/login路线上有一个登录页面。

在我网站的其他公共页面上 ,我想在每个页面的顶部放一个小的登录表单。

我该怎么做?

在app / Resources / config中 /routing.yml

nn
index:
path:/index 
defaults:{_ control:MyBundle:MyController:MyAction} 
 
 #########  ######### 
#FOS用户包
 ################## 
 nfos_user_security:
资源:“@ FOSUserBundle / Resources / config /  routing / security.xml“
   
 
 

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

   {%extends“:MyLayout.html.twig”%} 
 {%block fos_user_content%} 
 {%if error error} 
&lt; div class =“alert alert-danger”&gt; {{error  | trans({},'FOSUserBundle')}}&lt; / div  &gt; 
 {%endif%} 
 
&lt; form action =“{{path(”fos_user_security_check“)}}”method =“post”&gt; 
&lt; input type =“hidden”name =“_ csrf_token  “value =”{{csrf_token}}“/&gt; 
&lt; div class =”row“&gt; 
&lt; div class =”form-group col-m-6“&gt; 
&lt;输入类型 =“text”class =“_ username”id =“_ username”name =“_ username”value =“{{last_username}}”placeholder =“Email ou identificationiant”required =“required”/&gt; 
&lt; / div&gt;  
&lt; div class =“form-group col-m-6”&gt; 
&lt; input type =“password”class =“_ password”id =“_ password”name =“_ password”placeholder =“Mot de passe  “required =”required“/&gt; 
&lt; / div&gt; 
&lt; / div&gt; 
 
&lt; div class =”form_action“&gt; 
&lt; button type =”submit“class =” 按钮button-default-outline“id =”_ submit“name =”_ submit“value =”{{'security.login.submit'| trans({},'FOSUserBundle')}}“&gt; Connexion&lt; / button&gt; \  n&lt; div class =“__ sub”&gt; 
&lt; label class =“remember”&gt; 
&lt; input type =“checkbox”id =“remember_me”name =“_ rem  ember_me“已检查&gt;  {{'security.login.remember_me'| trans({},'FOSUserBundle')}} 
&lt; span class =“separator”&gt; |&lt; / span&gt; 
&lt; a href =“#”class  =“忘记”&gt; Mot depasseoublié?&lt; / a&gt; 
&lt; / label&gt; 
&lt; / div&gt; 
&lt; / div&gt; 
 
&lt; / form&gt; 
 {%endblock  fos_user_content%} 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题