0

I'm trying to implement the register part of a project in Symfony with the Sonata Bundle but i get the exception "Cannot redirect to an empty URL". The error is generated in the controller file RegistrationFOSUser1Controller in the registerAction.

public function registerAction()
{
    $user = $this->container->get('security.context')->getToken()->getUser();

    if ($user instanceof UserInterface) {
        $this->container->get('session')->getFlashBag()->set('sonata_user_error', 'sonata_user_already_authenticated');
        $url = $this->container->get('router')->generate('booster_bici_homepage');

        return new RedirectResponse($url);
    }

    $form = $this->container->get('sonata.user.registration.form');
    $formHandler = $this->container->get('sonata.user.registration.form.handler');
    $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');

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

        $authUser = false;
        if ($confirmationEnabled) {
            $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
            $url = $this->container->get('router')->generate('fos_user_registration_check_email');
        } else {
            $authUser = true;
            $route = $this->container->get('session')->get('sonata_basket_delivery_redirect');

            if (null !== $route) {
                $this->container->get('session')->remove('sonata_basket_delivery_redirect');
                $url = $this->container->get('router')->generate($route);
            } else {
                $url = $this->container->get('session')->get('sonata_user_redirect_url');//IT SEEMS THAT HERE IS RETURNED NULL
            }
        }

        $this->setFlash('fos_user_success', 'registration.flash.user_created');



        $response = new RedirectResponse($url);// THIS IS THE LINE OF THE ERROR

        if ($authUser) {
            $this->authenticateUser($user, $response);
        }

        return $response;
    }

    $this->container->get('session')->set('sonata_user_redirect_url', $this->container->get('request')->headers->get('referer'));

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

The part where the error is generated is :

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

        $authUser = false;
        if ($confirmationEnabled) {
            $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
            $url = $this->container->get('router')->generate('fos_user_registration_check_email');
        } else {
            $authUser = true;
            $route = $this->container->get('session')->get('sonata_basket_delivery_redirect');

            if (null !== $route) {
                $this->container->get('session')->remove('sonata_basket_delivery_redirect');
                $url = $this->container->get('router')->generate($route);
            } else {
                $url = $this->container->get('session')->get('sonata_user_redirect_url'); //IT SEEMS THAT HERE IS RETURNED NULL
            }
        }

        $this->setFlash('fos_user_success', 'registration.flash.user_created');



        $response = new RedirectResponse($url);// THIS IS THE LINE OF THE ERROR

I can see that the problem is that $url is empty. I have traced the problem to the line:

 $url = $this->container->get('session')->get('sonata_user_redirect_url');

It seems that $this->container->get('session')->get('sonata_user_redirect_url') returns null but i don't know what to do in order to avoid that or what i'm doing wrong.

Any ideas?

Rafael Bermúdez
  • 123
  • 4
  • 16

1 Answers1

0

just change:

$url = $this->container->get('session')->get('sonata_user_redirect_url');

with:

$url = $this->generateUrl('sonata_user_profile_show');
Marek Skiba
  • 2,124
  • 1
  • 28
  • 31