简体   繁体   English

FOSUserBundle注册定制

[英]FOSUserBundle registration customize

What I am trying to do is nothing hard. 我想做的事情并不难。 This is my first project on symfony and it's really confusing. 这是我关于symfony的第一个项目,这真的令人困惑。

I am using FOSUSerbundle. 我正在使用FOSUSerbundle。 I dont want to have a login and registration bellow /login and /registration 我不希望登录和注册下面/login/registration

So I made a bundle which is child of FOSUSerbundle ... and it overrides its twigs. 所以我做了一个捆绑,这是FOSUSerbundle的孩子......它覆盖了它的树枝。

I have ::base.html.twig where I include header.html.twig and there I have: {% render 'FOSUserBundle:Security:login' %} which render my teplate (overrided the FOS one) works gr8. 我有:: base.html.twig,其中包含header.html.twig,我有: {% render 'FOSUserBundle:Security:login' %} ,它使我的teplate(覆盖了FOS)工作gr8。 Even the errors after submiting are rendering on the ::base template bellow "/" route. 即使提交后的错误也会在:: base模板下呈现“/”路径。

#Security.yml
    form_login:
        check_path: /login_check
        login_path: /
        provider: fos_userbundle

Works great. 效果很好。

And I need to do exactly that same for my registration. 我需要为我的注册做同样的事情。

So in ::base I include welcome_page.html.twig where I code {% render 'FOSUserBundle:Registration:register' %} and there I have under my rewrited template: WelcomePageBundle:Registration:register.html.twig this: 所以在::base我包含welcome_page.html.twig ,其中我编码{% render 'FOSUserBundle:Registration:register' %} ,我在我的重写模板下: WelcomePageBundle:Registration:register.html.twig this:

{% block fos_user_content %}
{% include "FOSUserBundle:Registration:register_content.html.twig" %}
{% endblock fos_user_content %}[/code]

which also include from MY rewrited bundle: WelcomePageBundle:Registration:register_content.html.twig this: 其中还包括MY rewrited bundle: WelcomePageBundle:Registration:register_content.html.twig this:

{% for key, message in app.session.getFlashes() %}
<div class="{{ key }}">
    {{ message|trans({}, 'FOSUserBundle') }}
</div>
{% endfor %}

<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" id="register_form">
    {{ form_widget(form) }}
    {{ form_rest(form) }}

    <input type="submit" class="registration_submit" value="{{     'welcome_page.registration_box.register_submit'|trans }}"/>
</form>

<div class="v_pripade">
    {{ 'welcome_page.registration_box.with_reg_problems'|trans }}
    <span style='color: #fff568'>{{ 'welcome_page.registration_box.with_reg_problems_part2'|trans }}</span>
</div>

Everything works like a charm... all the files are included and displayed greate. 一切都像魅力一样...所有文件都包含在内并显示出来。 But the problem comes now. 但问题来了。

When I go to route /register 当我去路线/register

(which is basic route from FOS bundle) (这是FOS捆绑的基本路线)

<route id="fos_user_registration_register" pattern="/register">
  <default key="_controller">FOSUserBundle:Registration:register</default>
</route>

... fill data and click submit... it works. ...填写数据并单击提交...它的工作原理。 The errors are displayed or registration is succes.. 显示错误或注册成功..

But when I submit form from my route / where the registration controller is rendered (rendered ok) it takes my to this route :/register which is normal behaviour because this path: 但是当我从我的路径/提交注册控制器的地方提交表单(呈现正常)时,我需要这条路径:/register这是正常的行为,因为这条路径:

<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form)    }} method="POST" id="register_form">

... this site isn't extended by nothing so its just clean form on white page with errors... OK ...这个网站没有任何扩展,所以它只是在白页上干净的形式有错误......好的

But how can I possible make work this form with displaying errors and success ON MY ::base template like the login? 但是,我怎么可能在这个表单上工作,显示错误并成功登录MY :: base模板,如登录? and dont go to /register route? 并且不去/register路线? I tried replace /register for / which bring me to my ::base template (like in login I do). 我尝试替换/register /将我带到我的::base模板(就像我登录时一样)。

#security.yml
form_login:
    check_path: /login_check
    login_path: /
    provider: fos_userbundle

But none of the errors or success are displayed ... 但没有显示错误或成功......

Do anyone know solution? 有人知道解决方案吗?

You'll find the official documentation about how to override default FOSUserBundle controllers at http://symfony.com/doc/current/bundles/FOSUserBundle/overriding_controllers.html 您可以在http://symfony.com/doc/current/bundles/FOSUserBundle/overriding_controllers.html找到有关如何覆盖默认FOSUserBundle控制器的官方文档。

Create a controller for your homepage and forward requests ( http://symfony.com/doc/master/book/controller.html#forwarding ) to FOSUserBundle registration controller or add logic to your own controller after doing whatever FOSUserBundle does at registration: 为您的主页创建一个控制器并将请求转发到FOSUserBundle注册控制器( http://symfony.com/doc/master/book/controller.html#forwarding )或在完成FOSUserBundle在注册时执行的操作后向您自己的控制器添加逻辑:

<?php

namespace Acme\UserBundle\Controller;

// Imports @route annotation 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class CustomRegistrationController extends BaseController {

    /**
     * @Route("/")
     */
    public function register() {
        $response = $this->forward('FOSUserBundle:Registration:register');

        return $response;
    }
}

or 要么

<?php

namespace Acme\UserBundle\Controller;

use Symfony\Component\HttpFoundation\RedirectResponse;
use FOS\UserBundle\Controller\RegistrationController as BaseController;

class RegistrationController extends BaseController
{
    public function registerAction()
    {
        $form = $this->container->get('fos_user.registration.form');
        $formHandler = $this->container->get('fos_user.registration.form.handler');
        $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');

        $process = $formHandler->process($confirmationEnabled);
        if ($process) {
            $user = $form->getData();

            /*****************************************************
             * Add new functionality (e.g. log the registration) *
             *****************************************************/
            $this->container->get('logger')->info(
                sprintf('New user registration: %s', $user)
            );

            if ($confirmationEnabled) {
                $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
                $route = 'fos_user_registration_check_email';
            } else {
                $this->authenticateUser($user);
                $route = 'fos_user_registration_confirmed';
            }

            $this->setFlash('fos_user_success', 'registration.flash.user_created');
            $url = $this->container->get('router')->generate($route);

            return new RedirectResponse($url);
        }

        return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
            'form' => $form->createView(),
        ));
    }
}

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

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