无法在模块init() - 方法中附加事件

I read about some of the best practices for ZF2. There, it was explained to attach the events from MVC in the init()-Method of the module's Module class:

class Module {

  public function getAutoloaderConfig()  {
      return array(
        'Zend\Loader\ClassMapAutoloader' => array(
          __DIR__ . '/autoload_classmap.php',
        ),
      );
    }

    public function init(ModuleManager $moduleManager) {
      echo 'init<br>';
      $em = $moduleManager->getEventManager();
      $em->attach(MvcEvent::EVENT_DISPATCH, array($this, 'onDispatch'));
      $em->attach(MvcEvent::EVENT_ROUTE, array($this, 'onRoute'));
    }

    public function onDispatch(MvcEvent $e){
      echo 'onDispatch<br>';
    }
    ...

It results in getting no error, nice. But the event is not caught...

Any ideas? I tried the SharedManager too, but it only worked for the EVENT_DISPATCH ...

See http://akrabat.com/zend-framework-2/module-specific-bootstrapping-in-zf2/

Only found this solution: attach listeners in onBootstrap()-Method in the Module.php class:

...

public function onBootstrap(MvcEvent $e){
 echo 'onBootstrap<br>';
 $em =  $e->getApplication()->getEventManager();
 $em->attach(MvcEvent::EVENT_DISPATCH, array($this, 'onDispatch'));
 $em->attach(MvcEvent::EVENT_ROUTE, array($this, 'onRoute'));
}

...

You'll be better using the shared manager. The example bellow is for disabling layout when we got a xmlHttpRequest and the priority -95 is a key point to make things work.

public function onBootstrap(MvcEvent $e) {
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);

    // Hybrid view for ajax calls (disable layout for xmlHttpRequests)
    $eventManager->getSharedManager()->attach('Zend\Mvc\Controller\AbstractController', MvcEvent::EVENT_DISPATCH, function(MvcEvent $event){
       /**
        * @var Request $request
        */
        $request = $event->getRequest();
        $viewModel = $event->getResult();

       if($request->isXmlHttpRequest()) {
            $viewModel->setTerminal(true);
        }

        return $viewModel;
    }, -95); 
}

Unless for specific cases, it's better to register your events in onBootstrap.

init is for "early events".

I found a link that is quite clear : http://samsonasik.wordpress.com/2013/03/30/zend-framework-2-getting-closer-with-eventmanager/

You can find the order of defaults MVC events in Zend\ModuleManager\Listener\DefaultListenerAggregate::attch :

 public function attach(EventManagerInterface $events)
    {
        $options                     = $this->getOptions();
        $configListener              = $this->getConfigListener();
        $locatorRegistrationListener = new LocatorRegistrationListener($options);

        // High priority, we assume module autoloading (for FooNamespace\Module classes) should be available before anything else
        $this->listeners[] = $events->attach(new ModuleLoaderListener($options));
        $this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE_RESOLVE, new ModuleResolverListener);
        // High priority, because most other loadModule listeners will assume the module's classes are available via autoloading
        $this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, new AutoloaderListener($options), 9000);

        if ($options->getCheckDependencies()) {
            $this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, new ModuleDependencyCheckerListener, 8000);
        }

        $this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, new InitTrigger($options));
        $this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, new OnBootstrapListener($options));
        $this->listeners[] = $events->attach($locatorRegistrationListener);
        $this->listeners[] = $events->attach($configListener);
        return $this;
    }