1

I try to override REGISTRATION_SUCCESS in FosUserBundle to redirect the admin on user's list after register a new user.

So I have created a new event subscriber :

<?php
namespace AppBundle\EventListener;

use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Mailer\MailerInterface;
use FOS\UserBundle\Util\TokenGeneratorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class RedirectAfterRegistrationSubscriber implements EventSubscriberInterface
{
    private $mailer;
    private $tokenGenerator;
    private $router;

    public function __construct(MailerInterface $mailer, TokenGeneratorInterface $tokenGenerator, UrlGeneratorInterface $router)
    {
        $this->mailer = $mailer;
        $this->tokenGenerator = $tokenGenerator;
        $this->router = $router;
    }

    public function onRegistrationSuccess(FormEvent $event)
    {
        $user = $event->getForm()->getData();

        $user->setEnabled(false);
        if (null === $user->getConfirmationToken()) {
            $user->setConfirmationToken($this->tokenGenerator->generateToken());
        }

        $this->mailer->sendConfirmationEmailMessage($user);

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

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

and the following service :

app.redirect_after_registration_subscriber:
    class: AppBundle\EventListener\RedirectAfterRegistrationSubscriber
    arguments: ['@fos_user.mailer', '@fos_user.util.token_generator', '@router']
    tags:
        - { name: kernel.event_subscriber }

I don't understand why this error appears :

Cannot autowire service "AppBundle\EventListener\RedirectAfterRegistrationSubscriber": 
argument "$mailer" of method "__construct()" references interface 
"FOS\UserBundle\Mailer\MailerInterface" but no such service exists. You should maybe alias
this interface to one of these existing services: "fos_user.mailer.default",
"fos_user.mailer.twig_swift", "fos_user.mailer.noop".
yivi
  • 42,438
  • 18
  • 116
  • 138
  • Did you try `'@fos_user.mailer.twig_swift` instead of `'@fos_user.mailer'` ? – domagoj Feb 24 '19 at 20:56
  • 1
    Your answer is in the error message `You should maybe alias this interface to one of these existing services: "fos_user.mailer.default", "fos_user.mailer.twig_swift", "fos_user.mailer.noop".` Have ou tried any of them? – Preciel Feb 24 '19 at 22:18
  • Thanks for your answers. @Domagoj : I have tried with '@fos_user.mailer.twig_swift' but the problem still exists. – Denis Ancelle Feb 25 '19 at 09:01
  • @Preciel : If aliases service consists to add in services.yml : fos_user.mailer alias : '@fos_user.mailer.twig_swift' and replacing '@fos_user.mail.twig_swift' by '@fos_user.mailer' in the service, I did it but aways the same error :( – Denis Ancelle Feb 25 '19 at 09:01

1 Answers1

1

I suppose you are using autodiscovering of services. Something like:

# services.yaml

AppBundle\:
    resource: '../src/'
...

So in addition to the @app.redirect_after_registration_subscriber that you define, Symfony defines another service with id @AppBundle\EventListener\RedirectAfterRegistrationSubscriber. Both point to AppBundle\EventListener\RedirectAfterRegistrationSubscriber class. Yet you configured the mailer parameter only for the first one.

The solution:

AppBundle\EventListener\RedirectAfterRegistrationSubscriber:
    arguments: ['@fos_user.mailer', '@fos_user.util.token_generator', '@router']
    tags:
        - { name: kernel.event_subscriber }

With autowiring and autoconfigure you can even sypmlify to:

AppBundle\EventListener\RedirectAfterRegistrationSubscriber:
    arguments:
        $mailer: '@fos_user.mailer'
VladRia
  • 1,475
  • 3
  • 20
  • 32
  • The problem is resolved! It seems that it is working only with the code you gave me so my question is, do i need to add my service too or just yours is necessary? Big thanks! – Denis Ancelle Feb 25 '19 at 11:11
  • There is no need for `app.redirect_after_registration_subscriber`, you should delete it. – VladRia Feb 25 '19 at 12:13
  • Okay, thanks you! I have another problem, in my configuration, an admin creates a user's account then an email is sent to the user. So the user in not logged to the website when he clicks on confirmation email. After clicking on link, he is redirected on the login form instead of confirmated page directly. I would be able to auto-log the user if possible. It is possible overriding registration_confirm or registration_confirmed listener?? – Denis Ancelle Feb 25 '19 at 20:41
  • https://stackoverflow.com/questions/9550079/how-to-programmatically-login-authenticate-a-user – VladRia Feb 26 '19 at 08:42