Easily attach listeners to ZF2's MVC events.
- Add
"mikemix/eventdispatcher": "1.*"
to your composer.json file - Run
php composer.phar self-update && php composer.phar update
- Add module
EventDispatcher
to your application modules in theapplication.config.php
file - Copy file
vendor/mikemix/eventdispatcher/config/event_dispatcher.global.php.dist
to theconfig/autoload/event_dispatcher.global.php
Congratulations. You are done and the library has been successfully installed (hope so). To subscribe to a mvc event, it is as simple as adding a name of your listener service name to the event_dispatcher.global.php
file. The name must be recognized by the service manager.
File 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
),
),
);
File module/Application/config/module.config.php
// ...
'service_manager' => array(
// ...
'invokables' => array(
'myDispatchListener' => 'Application\Listener\DispatchListener',
),
),
File module/Application/src/Application/Listener/DispatchListener.php
. For your convienience and type hinting in the editor of your choice, you can make listener implement the ListenerInterface interface, but you are not obliged to. Just make sure an onEvent()
method is callable.
<?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());
}
}