我在研究Symfony的活动。我希望每次新用户注册时都发送一封电子邮件
我创建了这些类
UserEvent.php
<?php
namespace Acme\UserBundle\Event;
final class UserEvents {
const NEW_USER='new.user';
} newUserEvent.php
<?php
/**
* EVENT DISPATCHER
*/
namespace Acme\UserBundle\Event;
use Acme\UserBundle\Entity\User;
use Symfony\Component\EventDispatcher\Event;
class NewUserEvent {
protected $user;
public function __construct (User $user) {
$this->user = $user;
}
public function getUser () {
return $this->user;
}
}newUserListener.php
<?php
namespace Acme\UserBundle\Event;
class NewUserListener {
public function sendEmailToUsers(NewUserEvent $event)
{
// ... send email to users
}
} 在我的控制器里
/**
* @Route("/test",name="test")
*/
public function testEvento(){
$em = $this->getEm();
$dispatcher = new EventDispatcher();
// attach listener
$listner = new NewUserListener();
$dispatcher->addListener(UserEvents::NEW_USER,array($listner,'sendEmailToUsers'));
$user = $em->getRepository('AcmeUserBundle:User')->findOneBy(array('username' => 'alex')); //mock
$event = new NewUserEvent($user);
$dispatcher->dispatch(UserEvents::NEW_USER,$event);
return new Response('hi');
}但我知道这个错误
可捕获的致命错误:传递给Symfony\Component\EventDispatcher\EventDispatcher::dispatch()的参数2必须是Symfony\Component\EventDispatcher\EventDispatcher\ContextErrorException的实例
我不明白我的错误
发布于 2014-12-08 16:08:43
class NewUserEvent必须扩展Event类(Symfony\Component\EventDispatcher\Event)event_dispatcher服务- $this->get('event_dispatcher')https://stackoverflow.com/questions/27361912
复制相似问题