Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subscriber call twice symfony

Tags:

symfony

I use FOSUserEvents after submit form but the subscriber call twice.

In this way my captcha is valid the first time and not valid the second

this is my code

<?php

namespace AppBundle\EventListener;



class CaptchaSubscriber implements EventSubscriberInterface
{

    private $router;
    private $requestStack;
    private $templating;

    /**
     * RedirectAfterRegistrationSubscriber constructor.
     */
    public function __construct(RouterInterface $router, RequestStack $requestStack, \Twig_Environment $templating)
    {
        $this->router = $router;
        $this->requestStack = $requestStack;
        $this->templating = $templating;

    }

    public function onRegistrationInit(GetResponseUserEvent $event)
    {

        if ($this->requestStack->getMasterRequest()->isMethod('post')) {
            
            ...handle captcha...

        }

    }

    public static function getSubscribedEvents()
    {
        return [
            FOSUserEvents::REGISTRATION_INITIALIZE => 'onRegistrationInit'
        ];
    }        
}

my symfony is 3.3

UPDATE

I added

        $event->stopPropagation();

with this snippet the code works, but i don't know if it is the best practice

like image 819
monkeyUser Avatar asked Sep 15 '25 13:09

monkeyUser


2 Answers

In my case of symfony 4.2 it depends on the service definition if it occures or not.

My Subscriber gets registered twice if I define the service like this:

# oauth process listener
app.subscriber.oauth:
    class: App\EventListenerSubscriber\OauthSubscriber
    arguments: ['@session', '@router', '@security.token_storage', '@event_dispatcher', '@app.entity_manager.user', '@app.fos_user.mailer.twig_swift']
    tags:
        - { name: kernel.event_subscriber }

But it gets registerd only once if I chenge the definition to this:

# oauth process listener
App\EventListenerSubscriber\OauthSubscriber:
    arguments: ['@session', '@router', '@security.token_storage', '@event_dispatcher', '@app.entity_manager.user', '@app.fos_user.mailer.twig_swift']
    tags:
        - { name: kernel.event_subscriber }

I posted a bug report on github and got immediately an answer, that in newer symfony versions event listeners and subscribers get registered automatically with their class name as key (under some default conditions - must read on that topic). So there is no need to register them explicitely as services. I we do this anyway, but using an arbitrary key instead of class name, there will be two services.

like image 152
Artur Cichosz Avatar answered Sep 18 '25 02:09

Artur Cichosz


If you are using Autowiring/Autoconfiguration, it's possible that you've added the subscriber service you show above, twice. I've done it myself when I first added the autowiring, but I also had the subscriber listed explicitly in the configuration as well.

You can see what events are registered (and check if any are registered more than once to perform the same service/action) with:

bin/console debug:event-dispatcher
like image 23
Alister Bulman Avatar answered Sep 18 '25 04:09

Alister Bulman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!