简体   繁体   English

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

[英]Symfony 2.8 FOSUserBundle Change the redirection after the password resetting

I'm following the FOSUserbundle Official documentation in order to change the redirection after a password resetting procedure. 我正在遵循FOSUserbundle官方文档 ,以便在密码重置过程后更改重定向。 I created a new class named PasswordResettingListener and add it to services.yml 我创建了一个名为PasswordResettingListener的新类,并将其添加到services.yml。

The class is executes up to the getSubscribedEvents() method but Symfony never calls my specific method that performs the redirect. 该类最多可以执行getSubscribedEvents()方法,但是Symfony从未调用我执行重定向的特定方法。 Am I misunderstanding something? 我误会了吗?

src/AppBundle/EventListener/PasswordResettingListener.php: src / AppBundle / EventListener / 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)); // ""
    }
}

The service is recorded here (/app/config/services.yml): # app/config/services.yml services: 该服务记录在此处(/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 }

src/AppBundle/EventListener/PasswordResettingListener.php: 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));
        }

    }

and app/config/services.yml services: 和app / config / services.yml服务:

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

You can use FOSUser's routing in order to change the redirect. 您可以使用FOSUser的路由来更改重定向。 When importing it's routes, you have two options: either you add all by adding 导入路线时,有两种选择:要么通过添加全部添加

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

or each of them, by path. 或每个按路径排列。 This is the one for the user's profile: 这是用于用户个人资料的一个:

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

Just change it's prefix by your desired path and that's all. 只需通过所需的路径更改其前缀即可,仅此而已。

PS: I tried to use Event Listener as well but it didn't worked. PS:我也尝试使用事件监听器,但没有用。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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