【发布时间】:2019-02-24 20:34:45
【问题描述】:
我尝试在注册新用户后覆盖 FosUserBundle 中的 REGISTRATION_SUCCESS 以重定向用户列表中的管理员。
所以我创建了一个新的事件订阅者:
<?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'
];
}
}
以及以下服务:
app.redirect_after_registration_subscriber:
class: AppBundle\EventListener\RedirectAfterRegistrationSubscriber
arguments: ['@fos_user.mailer', '@fos_user.util.token_generator', '@router']
tags:
- { name: kernel.event_subscriber }
我不明白为什么会出现这个错误:
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".
【问题讨论】:
-
你试过
'@fos_user.mailer.twig_swift而不是'@fos_user.mailer'吗? -
你的答案在错误信息
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".你试过了吗? -
感谢您的回答。 @Domagoj:我尝试过使用 '@fos_user.mailer.twig_swift' 但问题仍然存在。
-
@Preciel :如果别名服务包括添加 services.yml :fos_user.mailer 别名:'@fos_user.mailer.twig_swift' 并用'@fos_user 替换'@fos_user.mail.twig_swift'。 mailer' 在服务中,我做到了,但消除了同样的错误:(
标签: symfony fosuserbundle