用FOSUserBundle在同一页面中登录和注册[英] Login and register in same page with FOSUserBundle

本文是小编为大家收集整理的关于用FOSUserBundle在同一页面中登录和注册的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用登录名并在我的Symfony应用程序中的FosuserBundle中注册并在同一页面中注册.Login工作完美,但注册不起作用.当我单击按钮注册时,我重定向到登录.

这是我的登录.html.twig:

      {% extends "FOSUserBundle::layout.html.twig" %}

        {% trans_default_domain 'FOSUserBundle' %}

           {% block fos_user_content %}
         {% if error %}
           <div>{{ error.messageKey|trans(error.messageData, 'security') }}
           </div>
       {% endif %}

       <div class="login-container">

          <div class="position-relative">
            <div id="login-box" class="login-box visible widget-box no-border">
              <div class="widget-body">
                <div class="widget-main">
                  <h4 class="header blue lighter bigger">
                    <i class="ace-icon fa fa-coffee green"></i>
                    Please Enter Your Information
                  </h4>

                  <div class="space-6"></div>

                  <form method="post" action="{{ path("fos_user_security_check") }}">
                  <label class="block clearfix">
                        <span class="block input-icon input-icon-right">

                          <input type="text" name="_username" class="form-control" placeholder="{{ 'security.login.username'|trans }}" required="required" />
                          <i class="ace-icon fa fa-user"></i>
                        </span>
                      </label>

                      <label class="block clearfix">
                        <span class="block input-icon input-icon-right">

                          <input type="password" name="_password" class="form-control" value="" placeholder="{{ 'security.login.password'|trans }}" required="required" />
                          <i class="ace-icon fa fa-lock"></i>
                        </span>
                      </label>

                      <div class="space"></div>

                      <div class="clearfix">
                        <label class="inline">
                          <input type="checkbox" class="ace" name="_remember_me" value="{{ 'security.login.remember_me'|trans }}"/>

                          <span class="lbl"> Remember Me</span>
                        </label>

                        <button type="submit" id="_submit" name="_submit"class="width-35 pull-right btn btn-sm btn-primary">
                          <i class="ace-icon fa fa-key"></i>
                          <input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />
                            {{ 'security.login.submit'|trans }}   
                        </button>

                      </div>

                      <div class="space-4"></div>

                  </form>

                </div><!-- /.widget-main -->

                <div class="toolbar clearfix">
                  <div>
                    <a href="#" data-target="#signup-box" class="user-signup-link">
                      I want to register
                      <i class="ace-icon fa fa-arrow-right"></i>
                    </a>
                  </div>
                </div>
              </div><!-- /.widget-body -->
            </div><!-- /.login-box -->
            {{ render(controller('FOSUserBundle:Registration:register')) }}
          </div><!-- /.position-relative -->
      </div>

        {% endblock fos_user_content %}

这是我的寄存器.html.twig:

       <div id="signup-box" class="signup-box widget-box no-border">
              <div class="widget-body">
                <div class="widget-main">
                  <h4 class="header green lighter bigger">
                    <i class="ace-icon fa fa-users blue"></i>
                    New User Registration
                  </h4>

                  <div class="space-6"></div>
                  <p> Enter your details to begin: </p>
                  {% block fos_user_content %}
                  {% include "FOSUserBundle:Registration:register_content.html.twig" %}
                                  {% endblock fos_user_content %} 
                </div>

                <div class="toolbar center">
                  <a href="#" data-target="#login-box" class="back-to-login-link">
                    <i class="ace-icon fa fa-arrow-left"></i>
                    Back to login
                  </a>
                </div>
              </div><!-- /.widget-body -->
            </div><!-- /.signup-box -->

这是我的register_content.html.twig:

             {% trans_default_domain 'FOSUserBundle' %}

              {{ form_start(form, {'attr': {'class': 'form-horizontal'} },{'method': 'post', 'action': path('fos_user_registration_register'), 'attr': {'class': 'fos_user_registration_register'}}) }}


                    <fieldset>
                      <label class="block clearfix">
                        <span class="block input-icon input-icon-right">
                        {{ form_widget(form.email,{'attr': {'class': 'form-control'} }) }}

                          <i class="ace-icon fa fa-envelope"></i>
                        </span>
                      </label>

                      <label class="block clearfix">
                        <span class="block input-icon input-icon-right">
                         {{ form_widget(form.username,{'attr': {'class': 'form-control'} }) }}
                          <i class="ace-icon fa fa-user"></i>
                        </span>
                      </label>

                      <label class="block clearfix">
                        <span class="block input-icon input-icon-right">
                          {{ form_widget(form.plainPassword.first,{'attr': {'class': 'form-control'} }) }}
                          <i class="ace-icon fa fa-lock"></i>
                        </span>
                      </label>

                      <label class="block clearfix">
                        <span class="block input-icon input-icon-right">
                          {{ form_widget(form.plainPassword.second,{'attr': {'class': 'form-control'} }) }}
                          <i class="ace-icon fa fa-retweet"></i>
                        </span>
                      </label>

                      <label class="block">
                        <input type="checkbox" class="ace" />
                        <span class="lbl">
                          I accept the
                          <a href="#">User Agreement</a>
                        </span>
                      </label>

                      <div class="space-24"></div>

                      <div class="clearfix">
                        <button type="reset" class="width-30 pull-left btn btn-sm">
                          <i class="ace-icon fa fa-refresh"></i>
                          <span class="bigger-110">Reset</span>
                        </button>

                        <input type="submit" value="{{ 'registration.submit'|trans }}" />
                      </div>
                    </fieldset>
                  {{ form_end(form) }}

推荐答案

我通过在我的register_content.html.twig中删除{'attr': {'class': 'form-horizontal'} }来解决我的问题.

本文地址:https://www.itbaoku.cn/post/2090845.html

问题描述

I am using login and register in same page with fosuserBundle in my Symfony application.Login work perfect but register does not work.When I click button register I redirected to login.No registration in my database.

This is my login.html.twig:

      {% extends "FOSUserBundle::layout.html.twig" %}

        {% trans_default_domain 'FOSUserBundle' %}

           {% block fos_user_content %}
         {% if error %}
           <div>{{ error.messageKey|trans(error.messageData, 'security') }}
           </div>
       {% endif %}

       <div class="login-container">

          <div class="position-relative">
            <div id="login-box" class="login-box visible widget-box no-border">
              <div class="widget-body">
                <div class="widget-main">
                  <h4 class="header blue lighter bigger">
                    <i class="ace-icon fa fa-coffee green"></i>
                    Please Enter Your Information
                  </h4>

                  <div class="space-6"></div>

                  <form method="post" action="{{ path("fos_user_security_check") }}">
                  <label class="block clearfix">
                        <span class="block input-icon input-icon-right">

                          <input type="text" name="_username" class="form-control" placeholder="{{ 'security.login.username'|trans }}" required="required" />
                          <i class="ace-icon fa fa-user"></i>
                        </span>
                      </label>

                      <label class="block clearfix">
                        <span class="block input-icon input-icon-right">

                          <input type="password" name="_password" class="form-control" value="" placeholder="{{ 'security.login.password'|trans }}" required="required" />
                          <i class="ace-icon fa fa-lock"></i>
                        </span>
                      </label>

                      <div class="space"></div>

                      <div class="clearfix">
                        <label class="inline">
                          <input type="checkbox" class="ace" name="_remember_me" value="{{ 'security.login.remember_me'|trans }}"/>

                          <span class="lbl"> Remember Me</span>
                        </label>

                        <button type="submit" id="_submit" name="_submit"class="width-35 pull-right btn btn-sm btn-primary">
                          <i class="ace-icon fa fa-key"></i>
                          <input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />
                            {{ 'security.login.submit'|trans }}   
                        </button>

                      </div>

                      <div class="space-4"></div>

                  </form>

                </div><!-- /.widget-main -->

                <div class="toolbar clearfix">
                  <div>
                    <a href="#" data-target="#signup-box" class="user-signup-link">
                      I want to register
                      <i class="ace-icon fa fa-arrow-right"></i>
                    </a>
                  </div>
                </div>
              </div><!-- /.widget-body -->
            </div><!-- /.login-box -->
            {{ render(controller('FOSUserBundle:Registration:register')) }}
          </div><!-- /.position-relative -->
      </div>

        {% endblock fos_user_content %}

This is my register.html.twig:

       <div id="signup-box" class="signup-box widget-box no-border">
              <div class="widget-body">
                <div class="widget-main">
                  <h4 class="header green lighter bigger">
                    <i class="ace-icon fa fa-users blue"></i>
                    New User Registration
                  </h4>

                  <div class="space-6"></div>
                  <p> Enter your details to begin: </p>
                  {% block fos_user_content %}
                  {% include "FOSUserBundle:Registration:register_content.html.twig" %}
                                  {% endblock fos_user_content %} 
                </div>

                <div class="toolbar center">
                  <a href="#" data-target="#login-box" class="back-to-login-link">
                    <i class="ace-icon fa fa-arrow-left"></i>
                    Back to login
                  </a>
                </div>
              </div><!-- /.widget-body -->
            </div><!-- /.signup-box -->

This is my register_content.html.twig:

             {% trans_default_domain 'FOSUserBundle' %}

              {{ form_start(form, {'attr': {'class': 'form-horizontal'} },{'method': 'post', 'action': path('fos_user_registration_register'), 'attr': {'class': 'fos_user_registration_register'}}) }}


                    <fieldset>
                      <label class="block clearfix">
                        <span class="block input-icon input-icon-right">
                        {{ form_widget(form.email,{'attr': {'class': 'form-control'} }) }}

                          <i class="ace-icon fa fa-envelope"></i>
                        </span>
                      </label>

                      <label class="block clearfix">
                        <span class="block input-icon input-icon-right">
                         {{ form_widget(form.username,{'attr': {'class': 'form-control'} }) }}
                          <i class="ace-icon fa fa-user"></i>
                        </span>
                      </label>

                      <label class="block clearfix">
                        <span class="block input-icon input-icon-right">
                          {{ form_widget(form.plainPassword.first,{'attr': {'class': 'form-control'} }) }}
                          <i class="ace-icon fa fa-lock"></i>
                        </span>
                      </label>

                      <label class="block clearfix">
                        <span class="block input-icon input-icon-right">
                          {{ form_widget(form.plainPassword.second,{'attr': {'class': 'form-control'} }) }}
                          <i class="ace-icon fa fa-retweet"></i>
                        </span>
                      </label>

                      <label class="block">
                        <input type="checkbox" class="ace" />
                        <span class="lbl">
                          I accept the
                          <a href="#">User Agreement</a>
                        </span>
                      </label>

                      <div class="space-24"></div>

                      <div class="clearfix">
                        <button type="reset" class="width-30 pull-left btn btn-sm">
                          <i class="ace-icon fa fa-refresh"></i>
                          <span class="bigger-110">Reset</span>
                        </button>

                        <input type="submit" value="{{ 'registration.submit'|trans }}" />
                      </div>
                    </fieldset>
                  {{ form_end(form) }}

推荐答案

I solved my problem by deleting {'attr': {'class': 'form-horizontal'} } in my register_content.html.twig.