FOSUserBundle: Success target after password reset

女生的网名这么多〃 提交于 2019-11-30 12:53:07

问题


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?


回答1:


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 }



回答2:


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


来源:https://stackoverflow.com/questions/15878341/fosuserbundle-success-target-after-password-reset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!