Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override FOSUserBundle's EmailConfirmationListener

I activated user confirmation for FOSUserBundle. But I don't want to take the response from the original listener

$url = $this->router->generate('fos_user_registration_check_email');
$event->setResponse(new RedirectResponse($url));

I want to chose another route. I tried to extend the EventListener

namespace Acme\MyBundle\EventListener;

use FOS\UserBundle\EventListener\EmailConfirmationListener as BaseListener;
// ...

class EmailConfirmationListener extends BaseListener
{
    public function onRegistrationSuccess(FormEvent $event)
    {    
        $url = $this->router->generate('fos_user_registration_check_email');
        $event->setResponse(new RedirectResponse($url));
    }
}

Unfortunately, EventListeners don't seem to be extendable, just as Controllers or Forms are. (Just in case you wonder: of course my bundle is a child of the FOSUserBundle.)

So I want to avoid editing those two lines directly in the vendor folder (as it would be very bad practice to do so!). So what are my ways out of this calamity?

like image 262
Gottlieb Notschnabel Avatar asked Sep 11 '13 15:09

Gottlieb Notschnabel


1 Answers

Just override the service fos_user.listener.email_confirmation by creating a service with the same name in your config.yml ...

# app/config/config.yml

services:
    fos_user.listener.email_confirmation:
        class:        "Acme\MyBundle\EventListener\EmailConfirmationListener"
        arguments:    ["@fos_user.mailer", "@fos_user.util.token_generator", "@router", "@session"]
        tags:
            - { name: kernel.event_subscriber }

... or even cleaner - create a parameter that's being used by your service:

parameters:
    my.funky_parameter.class: "Acme\MyBundle\EventListener\EmailConfirmationListener"

services:
    fos_user.listener.email_confirmation:
        class: "%my.funky_parameter.class%"
        # ...

... or inside your bundle's xml/yml/php configuration file loaded by the bundle's extension. Make sure your bundle is being registered after FOSUserBundle in AppKernel.php when choosing this way.

... or the best method:

change the original service's class name in a compiler pass as the documentation chapter How to Override any Part of a Bundle suggests.

Maybe take a dive into the chapter How to work with Compiler Passes before choosing this option.

like image 133
Nicolai Fröhlich Avatar answered Sep 22 '22 16:09

Nicolai Fröhlich