radnan / rdn-event
Zend Framework 2 事件监听器管理器
v1.0.0
2013-12-27 04:37 UTC
Requires
This package is not auto-updated.
Last update: 2024-09-24 02:11:35 UTC
README
RdnEvent ZF2 模块提供了一个事件监听器的服务定位器。
如何安装
-
使用
composer需求radnan/rdn-event包$ composer require radnan/rdn-event:1.* -
通过在您的
application.config.php文件中包含它来激活模块<?php return array( 'modules' => array( 'RdnEvent', // ... ), );
如何使用
只需使用 rdn_event_listeners 配置选项,通过 RdnEvent\Listener\ListenerManager 服务定位器配置您的监听器。监听器是实现了 Zend\EventManager\ListenerAggregateInterface 接口任何类。
<?php return array( 'rdn_event_listeners' => array( 'invokables' => array(), 'factories' => array(), ), );
如何创建监听器
在您的模块内部创建监听器,使用事件监听器服务定位器注册它们,最后通过在 rdn_event[listeners] 配置选项中包含它来附加监听器。
让我们在我们的 App 模块内部创建一个 hello world 监听器
1. 创建监听器类
通过扩展 RdnEvent\Listener\AbstractListener 创建 App\Listener\HelloWorld 类
<?php namespace App\Listener; use RdnConsole\Listener\AbstractListener; use Zend\EventManager\EventInterface; use Zend\EventManager\EventManagerInterface; use Zend\Mvc\MvcEvent; class HelloWorld extends AbstractListener { public function attach(EventManagerInterface $events) { $this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH, array($this, 'execute')); } public function execute(EventInterface $event) { var_dump('Hello World!'); } }
2. 使用服务定位器注册监听器
将以下内容放置在您的 module.config.php 文件中
<?php return array( 'rdn_event_listeners' => array( 'invokables' => array( 'App:HelloWorld' => 'App\Listener\HelloWorld', ), ), );
3. 将监听器附加到事件管理器
现在,将以下内容放置在您的 module.config.php 文件中
<?php return array( 'rdn_event' => array( 'listeners' => array( 'App:HelloWorld', ), ), );
这就完成了!模块将从服务定位器获取监听器并将其附加到应用程序的事件管理器。