Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FOSUserBundle registration customize

What I am trying to do is nothing hard. This is my first project on symfony and it's really confusing.

I am using FOSUSerbundle. I dont want to have a login and registration bellow /login and /registration

So I made a bundle which is child of FOSUSerbundle ... and it overrides its twigs.

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. Even the errors after submiting are rendering on the ::base template bellow "/" route.

#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:

{% 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:

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

(which is basic route from FOS bundle)

<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:

<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? and dont go to /register route? I tried replace /register for / which bring me to my ::base template (like in login I do).

#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?

like image 275
Lukas Lukac Avatar asked Feb 14 '13 12:02

Lukas Lukac


1 Answers

You'll find the official documentation about how to override default FOSUserBundle controllers at http://symfony.com/doc/current/bundles/FOSUserBundle/overriding_controllers.html

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:

<?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(),
        ));
    }
}
like image 70
Thierry Marianne Avatar answered Oct 13 '22 01:10

Thierry Marianne