简体   繁体   English

Symfony3 在 FOSUserBundle 上的一个模板中登录和注册表格

[英]Symfony3 login and register form in one template on FOSUserBundle

I need to put in the same page the login form and the registration form.我需要将登录表单和注册表单放在同一页面中。 I'm using Symfony 3.2 and FOS User Bundle.我正在使用 Symfony 3.2 和 FOS 用户包。

I found this: How to merge login and register form in one template on FOSUserBundle我发现了这个: 如何在 FOSUserBundle 上的一个模板中合并登录和注册表单

Which was my first approach, but there is a problem.这是我的第一种方法,但有一个问题。 On validation error, the page gets redirected to another route (with only the template of the submitted form displaying).在验证错误时,页面被重定向到另一个路由(仅显示提交表单的模板)。 The problem is that the SecurityController and RegistrationController doesn't know of the controller which renders both FOS controllers in Twig, they just display the form template.问题是 SecurityController 和 RegistrationController 不知道在 Twig 中呈现两个 FOS 控制器的控制器,它们只是显示表单模板。 With this approach, I can't override the template the fos controllers are displaying to display the main template, becuase that would generate an infinite recursion.使用这种方法,我无法覆盖 fos 控制器显示的模板以显示主模板,因为这会产生无限递归。

I tried overriding the fos controllers to do a redirect on a validation error of the form, but the redirect makes the validation messages get lost.我尝试覆盖 fos 控制器以对表单的验证错误进行重定向,但重定向使验证消息丢失。

I also tried forwarding the request to both fos SecurityController and RegistrationController ( like this: http://symfony.com/doc/current/controller/forwarding.html ) but for the check action of the login, I get:我还尝试将请求转发给 fos SecurityController 和 RegistrationController(像这样: http : //symfony.com/doc/current/controller/forwarding.html )但是对于登录的检查操作,我得到:

RuntimeException 'You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.'. RuntimeException '您必须在安全防火墙配置中使用 form_login 配置要由防火墙处理的检查路径。'。

It's already configured and was working, but forwarding the request directly to the controller seems to break it (I couldn't figure out how is the login check being procesed really, as the check method returns only a runtime exception)它已经配置并且正在工作,但是将请求直接转发到控制器似乎破坏了它(我无法弄清楚登录检查是如何真正处理的,因为检查方法只返回一个运行时异常)

The last solution I could think of is to create both forms in the new controller, but I don't know how to call the fos user login check manually (for the registration I could just copy all the registerAction from RegistrationController).我能想到的最后一个解决方案是在新控制器中创建两个表单,但我不知道如何手动调用 fos 用户登录检查(对于注册,我可以从 RegistrationController 复制所有 registerAction)。

Thanks for your time.谢谢你的时间。

Ok, I finally could get it working.好的,我终于可以让它工作了。 I made a new controller which renders the login form and the registration form using the output of the fos controllers.我制作了一个新的控制器,它使用 fos 控制器的输出呈现登录表单和注册表单。 The login form action route is login_check, the registration form action route is the same as the the route of the controller (the value of frontend_login)登录表单动作路由为login_check,注册表动作路由与控制器路由相同(frontend_login的值)

The controller:控制器:

/**
 * @Route("/ingresar", name="frontend_login")
 * @Method({"GET", "POST"}) 
 */
public function loginAndRegisterAction(Request $request){

    $login_response = $this->forward('FOSUserBundle:Security:login', array( $request ));
    $register_response = $this->forward('FOSUserBundle:Registration:register', array( $request ));

    return $this->render('frontend/usuario/login_register.html.twig', array(
        'login' => $login_response->getContent(),
        'register' => $register_response->getContent(),
        ));
}

In the template that displays the contents, display it raw (the controllers return the forms already rendererd as html in it's content)在显示内容的模板中,将其显示为原始(控制器在其内容中返回已经渲染为 html 的表单)

{{ login|raw }}
{{ register|raw }}

I have to override the FosUserBundle templates soy the don't extend the fosuserbundle layout.我必须覆盖 FosUserBundle 模板以便不扩展 fosuserbundle 布局。 The Resources/FOSUserBundle/layout.html.twig: Resources/FOSUserBundle/layout.html.twig:

{% block fos_user_content %}{% endblock fos_user_content %}

Configure the security.yml to indicate that the login path is the one of the controller we defined.配置security.yml,表明登录路径是我们定义的控制器之一。 On error it will display the frontend_login route.出错时,它将显示frontend_login路由。

frontend:
        pattern: ^/
        context: website
        form_login:
            provider: fos_userbundle
            login_path: frontend_login
            check_path: login_check

Finally, override the template displaying the form so that the route of the action of the registration form is frontend_login .最后,覆盖显示表单的模板,使注册表单的动作路径为frontend_login I do this way beacause I will need to change the html structure, I supposed that changing the fos_user_registration_register route to the one we defined should do the trick.我这样做是因为我需要更改 html 结构,我认为将fos_user_registration_register路由更改为我们定义的路由应该可以解决问题。

{{ form_start(form, {'method': 'post', 'action': path('frontend_login'), 'attr': {'class': 'fos_user_registration_register', 'novalidate': 'novalidate'}}) }}
    {{ form_widget(form) }}
    <div>
        <input type="submit" value="Submit" />
    </div>
{{ form_end(form) }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM