我正在尝试根据此处给出的示例设置一个简单的事件订阅 - http://symfony.com/doc/master/components/event_dispatcher/introduction.html。
这是我的活动商店:
namespace CookBook\InheritanceBundle\Event;
final class EventStore
{
const EVENT_SAMPLE = 'event.sample';
}
这是我的活动订阅者:
namespace CookBook\InheritanceBundle\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\Event;
class Subscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
var_dump('here');
return array(
'event.sample' => array(
array('sampleMethod1', 10),
array('sampleMethod2', 5)
));
}
public function sampleMethod1(Event $event)
{
var_dump('Method 1');
}
public function sampleMethod2(Event $event)
{
var_dump('Method 2');
}
}
这是 services.yml 中的配置:
kernel.subscriber.subscriber:
class: CookBook\InheritanceBundle\Event\Subscriber
tags:
- {name:kernel.event_subscriber}
以下是我提出事件的方式:
use Symfony\Component\EventDispatcher\EventDispatcher;
use CookBook\InheritanceBundle\Event\EventStore;
$dispatcher = new EventDispatcher();
$dispatcher->dispatch(EventStore::EVENT_SAMPLE);
预期输出:
string 'here' (length=4)
string 'Method 1' (length=8)
string 'Method 2' (length=8)
实际输出:
string 'here' (length=4)
出于某种原因,不会调用侦听器方法。有人知道这段代码有什么问题吗?谢谢。