Zend Framework 2 is an event driven framework, which means you can attach listeners/functions almost before/after anything.

The ModuleManager has some predefined listeners, such as:

AutoloaderListener (getAutloaderConfig)
ConfigListener (getConfig)
InitTrigger (init)
OnBootstrapListener (onBootstrap)

ModuleManager also inherits some events/ triggers /listeners from the ServiceManager, such as:

getServiceConfig
getControllerConfig
getControllerPluginConfig
getViewHelperConfig

But what about MVC events? How can we attach a function/listener from our module? MvcEvent has 6 event triggers (bootstrap, dispatch, dispatch.error, finish, render, route) and you can easily trigger a function for each of them from your module.

In my example im using the «dispatch»-trigger, open your module-file (in my case «module/Empirio/Module.php») and create a function «onDispatch» like this:

module/Empirio/Module.php
<?php
namespace Empirio;
// ...
class Module{

    // ... your code

    public function onDispatch(\Zend\Mvc\MvcEvent $e){
        // ... your logic here ...
    }
}

Now we want to attach this function when our application is dispatched, we do this from our onBootstrap-function, like this:

module/Empirio/Module.php
<?php
namespace Empirio;
// ...
class Module{

    // ... your code

    public function onBootstrap(\Zend\Mvc\MvcEvent $e){
        // ...

        $em = $e->getApplication()->getEventManager();
        $em->attach(\Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'onDispatch'));
    }
}

Or you can attach the function from the init-function as well, like this:

module/Empirio/Module.php
<?php
namespace Empirio;
// ...
class Module{

    // ... your code

    public function init(\Zend\ModuleManager\ModuleManager $mm){
        // ...

        $sem  = $mm->getEventManager()->getSharedManager();
        $sem->attach('application', \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'onDispatch'));
    }
}

As u can see the «init»-function uses ModuleManager, where «onBootstrap»-function uses MvcEvent as its parameters.