使用Symfony 2.5.3。我试图发送一个欢迎'有人使用EventListener成功注册(FOS Userbunde)时发送电子邮件。触发的事件是fos_user.registration.success
。
所以我添加了一项服务:
mycustom_user.registration_success:
class: Mycustom\UserBundle\EventListener\RegistrationListener
arguments: [@mycustom_user.mailer]
tags:
- { name: kernel.event_listener, event: fos_user_registration_success, method: onRegistrationSuccess}
听众本身:
namespace Mycustom\UserBundle\EventListener;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\Event\FormEvent;
use Mycustom\UserBundle\Mailer\Mailer;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class RegistrationListener implements EventSubscriberInterface
{
protected $mailer;
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
);
}
public function onRegistrationSuccess(FormEvent $event)
{
$user = $event->getForm()->getData();
$this->mailer->sendWelcomeMessage($user);
$url = $this->router->generate('fos_user_security_login');
$event->setResponse(new RedirectResponse($url));
}
}
邮件程序本身包含电子邮件内容的呈现,也注册为服务:
mycustom_user.mailer:
class: Mycustom\UserBundle\Mailer\Mailer
arguments: ['@templating']
mycustom_user.mailer
是听众的参数。但不知怎的,我不断收到这个错误:
Catchable Fatal Error: Argument 1 passed to
Mycustom\UserBundle\EventListener\RegistrationListener::__construct()
must be an instance of Mycustom\UserBundle\Mailer\Mailer, none given,
called in mycustom/app/cache/dev/appDevDebugProjectContainer.php
on line 2214 and defined in mycustom/src/Mycustom/UserBundle/EventListener/RegistrationListener.php line 19
我尝试了像@doctrine这样的其他参数(并相应地更改了侦听器构造函数),但我仍然遇到同样的错误。此外,邮件服务的@templating参数也不起作用。
我在这里做错了什么?
答案 0 :(得分:0)
所以,我遇到的问题是我在一个班级中有两个听众。第二个服务定义不包含构造函数的参数。应该是这样的:
mycustom_user.registration_initialize:
class: Mycustom\UserBundle\EventListener\RegistrationListener
arguments: ['@mycustom_user.mailer']
tags:
- { name: kernel.event_subscriber, alias: mycustom_user_registration_listener}
mycustom_user.registration_success:
class: Mycustom\UserBundle\EventListener\RegistrationListener
arguments: ['@mycustom_user.mailer']
tags:
- { name: kernel.event_subscriber }