Symfony 2 FOS用户无法重定向到空URL

时间:2016-01-11 15:18:32

标签: symfony fosuserbundle sonata-user-bundle

我尝试使用Sonata Bundle在Symfony中实现项目的注册部分,但是我得到了异常"无法重定向到空URL"。该错误在registerAction中的控制器文件RegistrationFOSUser1Controller中生成。

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(),
    ));
}

生成错误的部分是:

    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

我可以看到问题是$ url是空的。我已将问题追溯到该行:

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

似乎$ this-> container-> get(' session') - > get(' sonata_user_redirect_url')返回null但我不知道该怎么做才能避免这种情况或我做错了什么。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

只是改变:

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

使用:

$url = $this->generateUrl('sonata_user_profile_show');