GAV
GAV

Reputation: 1213

symfony EventSubscriber ignored in Symfony 3.4

I'm trying to use an event subscriber to redirect person registering to a different route, than the standard FOSUser bundle directs them to. Going by some tutorials for Symfony 3.3, but wondered as I have version 3.4 if anything needs changing to make it work, as it currently just goes to the standard page? Thanks

EventSubscriber

      namespace eventsBundle\EventListener;


      use Symfony\Component\EventDispatcher\EventSubscriberInterface;
      use Symfony\Component\Security\Http\Util\TargetPathTrait;
      use Symfony\Component\Routing\RouterInterface;
      use FOS\UserBundle\Event\FormEvent;
      use Symfony\Component\HttpFoundation\RedirectResponse;
      use FOS\UserBundle\FOSUserEvents;

      class RedirectAfterRegistrationSubscriber implements 
      EventSubscriberInterface
      {

      use TargetPathTrait;
      private $router;
      public function __construct(RouterInterface $router)
      {
      $this->router = $router;
      }

      public function onRegistrationSuccess(FormEvent $event)
      {
     die();
     // main is your firewall's name
     $url = $this->getTargetPath($event->getRequest()->getSession(), 
     'main');

     if (!$url) {
        $url = $this->router->generate('homepage');
     }

     $response = new RedirectResponse($url);
     $event->setResponse($response);
     }

     public static function getSubscribedEvents()
     {
     return [
        FOSUserEvents::REGISTRATION_SUCCESS => 
        ['onRegistrationSuccess',-5]
    ];
   }
  }

services.yml

 services:
         _defaults:
         autowire: true
         autoconfigure: true        

eventsBundle\:
    resource: '../../src/eventsBundle/*'
    exclude: '../../src/eventsBundle/{Entity,Repository,Tests}'

eventsBundle\EventListener\RedirectAfterRegistrationSubscriber:
    autowire: true

I added die(); just to make sure it was going to this but, has not effect

Upvotes: 2

Views: 269

Answers (2)

GAV
GAV

Reputation: 1213

The services.yml file to change is the one in my bundle not the main services.yml under app/config, this now picks up the EventSubscriber

Upvotes: 2

Ihor Kostrov
Ihor Kostrov

Reputation: 2561

Looks like you need to add tag for your subscriber.

eventsBundle\EventListener\RedirectAfterRegistrationSubscriber:
    autowire: true 
    tags:
        - { name: kernel.event_subscriber }

Upvotes: 0

Related Questions