mikemix/eventdispatcher

该软件包已被废弃,不再维护。没有建议的替代软件包。

轻松附加到ZF2事件

1.0.2 2014-06-11 19:50 UTC

This package is auto-updated.

Last update: 2019-08-19 07:04:38 UTC


README

此模块过时、糟糕且未维护,因此请勿使用它 :)

轻松将监听器附加到ZF2的MVC事件。

安装

  1. "mikemix/eventdispatcher": "1.*"添加到您的composer.json文件中
  2. 运行php composer.phar self-update && php composer.phar update
  3. 将模块EventDispatcher添加到application.config.php文件中的应用程序模块中
  4. 将文件vendor/mikemix/eventdispatcher/config/event_dispatcher.global.php.dist复制到config/autoload/event_dispatcher.global.php

恭喜,您已完成,库已成功安装(希望如此)。要订阅MVC事件,只需将您的监听器服务名称添加到event_dispatcher.global.php文件中。该名称必须被服务管理器识别。

示例配置

文件config/autoload/event_dispatcher.config.php

return array(
    'event_dispatcher' => array(
        'dispatch' => array(
            // dispatch listeners here
            'myDispatchListener'       # <----------------- NOTICE
        ),
        'dispatch.error' => array(
            // dispatch.error listeners here
        ),
        'finish' => array(
            // finish listeners here
        ),
        'render' => array(
            // render listeners here
        ),
        'render.error' => array(
            // render.error listeners here
        ),
        'route' => array(
            // route listeners here
        ),
    ),
);

文件module/Application/config/module.config.php

// ...
'service_manager' => array(
    // ...
    
    'invokables' => array(
        'myDispatchListener' => 'Application\Listener\DispatchListener',
    ),
),

文件module/Application/src/Application/Listener/DispatchListener.php。为了方便您在所选编辑器中的类型提示,您可以让监听器实现ListenerInterface接口,但这不是强制性的。只需确保有一个可调用的onEvent()方法即可。

<?php
namespace Application\Listener;

use EventDispatcher\Listener\ListenerInterface;

class DispatchListener implements ListenerInterface
{
    public function onEvent(EventInterface $event)
    {
        printf('Well hello, a %s event was called', $event->getName());
    }
}

关于ZF2事件在网络上的宝贵资源