【问题标题】:FOSUserBundle: Success target after password resetFOSUserBundle:密码重置后的成功目标
【发布时间】:2013-03-30 11:45:47
【问题描述】:

在用户使用 FOSUserBundle 的密码重置来重置他的密码后,默认情况下他会被重定向到 FOSUserProfile。 我想重定向到不同的路线。 这可能吗?如果可以,怎么做?

【问题讨论】:

    标签: symfony url-routing fosuserbundle


    【解决方案1】:

    可以通过创建一个重置订阅者来完成:

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

    然后将其注册为带有kernel.event_subscriber标签的服务:

    # src/Acme/UserBundle/Resources/config/services.yml
    services:
        acme_user.password_resetting:
            class: Acme\UserBundle\EventListener\PasswordResettingListener
            arguments: [ @router ]
            tags:
                - { name: kernel.event_subscriber }
    

    【讨论】:

    • 注意:此解决方案要求您使用 FOS Userbundle 的主版本。您可以通过扩展重置控制器并更改 getRedirectionUrl() 方法来获得类似的结果。
    • 注意:从 Symfony 4 开始,您不需要在 service.yml 中指定路由器参数。
    【解决方案2】:

    如果您不使用 FOS 用户配置文件视图,有一个丑陋但简单的方法:

    添加您的app/config/routing.yml:

    fos_user_profile_show:
        path: /yourpath
    

    【讨论】:

    • 你也可以在自己的控制器中声明路由,命名为fos_user_profile_show,比绝对url更好。
    • @LouTerrailloune 在上述路由配置中没有绝对 url。它可能看起来像,但它不是
    猜你喜欢
    • 1970-01-01
    • 2013-12-09
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 2019-06-04
    • 2011-04-03
    相关资源
    最近更新 更多