简体   繁体   中英

How to override confirm registration redirect and where email confirmation enabled is checked by FOSUserBundle?

1) FOSUserBundle registration redirects to confirmedAction bydefault

        if (null === $response = $event->getResponse()) {
            $url = $this->generateUrl('fos_user_registration_confirmed');
            $response = new RedirectResponse($url);
        }

        $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

        return $response;

I want to override this behaviour as I am calling registerAction using REST. I want to return confirmation message instead of redirecting.

FilterUserResponseEvent do not allow to set response, there is no "setResponse" method.

2) FOSUserBundle's EmailConfirmationListener also redirects to checkEmail action

    $url = $this->router->generate('fos_user_registration_check_email');
    $event->setResponse(new RedirectResponse($url));

I want to override this behavious as well. I could do this by creating my EmailConfirmationListener service with same id "fos_user.listener.email_confirmation" and by doing this

$response = new Response($this->translator->trans('registration.check_email', array(
                '%email%' => $user->getEmail()), 'FOSUserBundle'));
        $event->setResponse($response);

But the problem is now below configuration is not working. It always sends confirmation email regardless of below configuration.

fos_user
   registration 
       confirmation
           enabled : false

I couldn't see any code in EmailConfirmationListerner which checks whether this configuration is enabled or not. Where is it checked, why overriding Listener stops configuration working ?

For your first question, setting your own redirect response should be done on FOSUserEvents::REGISTRATION_SUCCESS rather than FOSUserEvents::REGISTRATION_COMPLETED . This is precisely why the code you pasted from the bundle is checking whether a response is set before creating its own RedirectResponse.

For your second question, the behaviour of the configuration setting is to register the confirmation listener or to skip registering in the bundle. If you register your own listener, you have to control the registration of your own listener on your side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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