FOSUserBundle: Success target after password reset

2019-02-02 15:15发布

After the user did reset his password using the password reset of FOSUserBundle, by default he is redirected to the FOSUserProfile. I want to redirect to a different route. Is this possible and if yes, how?

2条回答
Rolldiameter
2楼-- · 2019-02-02 16:03

In case you are not using the FOS user profile view, there is an ugly yet simple way:

Add in your app/config/routing.yml:

fos_user_profile_show:
    path: /yourpath
查看更多
贪生不怕死
3楼-- · 2019-02-02 16:06

It can be done by creating a resetting subscriber:

namespace Acme\UserBundle\EventListener;

use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
 * Listener responsible to change the redirection at the end of the password resetting
 */
class PasswordResettingListener implements EventSubscriberInterface {
    private $router;

    public function __construct(UrlGeneratorInterface $router) {
        $this->router = $router;
    }

    public static function getSubscribedEvents() {
        return [
            FOSUserEvents::RESETTING_RESET_SUCCESS => 'onPasswordResettingSuccess',
        ];
    }

    public function onPasswordResettingSuccess(FormEvent $event) {
        $url = $this->router->generate('homepage');
        $event->setResponse(new RedirectResponse($url));
    }
}

And then registering it as a service with kernel.event_subscriber tag:

# src/Acme/UserBundle/Resources/config/services.yml
services:
    acme_user.password_resetting:
        class: Acme\UserBundle\EventListener\PasswordResettingListener
        arguments: [ @router ]
        tags:
            - { name: kernel.event_subscriber }
查看更多
登录 后发表回答