Symfony 2.8 FOSUserBundle重置密码后更改重定向

时间:2016-02-17 16:51:52

标签: php symfony fosuserbundle

我跟随FOSUserbundle Official documentation以便在密码重置过程后更改重定向。我创建了一个名为PasswordResettingListener的新类,并将其添加到services.yml

该类执行getSubscribedEvents()方法,但Symfony从不调用执行重定向的特定方法。我误解了什么吗?

的src /的appbundle /事件监听/ PasswordResettingListener.php:

<?php 
// src/AppBundle/EventListener/PasswordResettingListener.php

namespace AppBundle\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;

class PasswordResettingListener implements EventSubscriberInterface {
    private $router;

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

    /**
    * CALLED!
    */
    public static function getSubscribedEvents() {
        return array(
        FOSUserEvents::RESETTING_RESET_SUCCESS => 'onPasswordResettingSuccess',
        );
    }

    public function onPasswordResettingSuccess(FormEvent $event) {
        die("alone"); //never called 
        $url = $this->router->generate('tarifs'); //nevercalled
        die("die alone !!!!!"); // never called 
        $event->setResponse(new RedirectResponse($url)); // ""
    }
}

此处记录服务(/app/config/services.yml):     #app / config / services.yml     服务:

app.form.registration:
    class: AppBundle\Form\RegistrationType
    tags:
        - { name: form.type, alias: app_user_registration }

app.password_resetting:
    class: AppBundle\EventListener\PasswordResettingListener
    arguments: [@router]
    tags:
        - { name: kernel.event_subscriber }

2 个答案:

答案 0 :(得分:1)

src / AppBundle / EventListener / PasswordResettingListener.php:

use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
    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;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

    class PasswordResettingListener implements EventSubscriberInterface
    {
        private $router;
        private $security;

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

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

    }

和app / config / services.yml服务:

  acme_user.password_resetting:
          class: src/AppBundle/EventListener/PasswordResettingListener
          arguments: [ "@router", "@security.authorization_checker" ]
          tags:
              - { name: kernel.event_subscriber }

答案 1 :(得分:0)

您可以使用FOSUser的路由来更改重定向。 导入路线时,您有两种选择:通过添加

添加全部

fos_user: resource: "@FOSUserBundle/Resources/config/routing/all.xml"

或他们每个人,按路径。这是用户个人资料的一个:

fos_user_profile:
resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
prefix: /profile

只需用你想要的路径改变它的前缀即可。

PS:我也尝试使用事件监听器,但它没有用。

祝你好运!