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

时间:2014-02-18 16:42:41

标签: php zend-framework2

我读到了ZF2的一些最佳实践。在那里,解释了在init()中附加来自MVC的事件 - 模块的模块类的方法:

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>';
    }
    ...

它导致没有错误,很好。但事件没有被发现......

有什么想法吗?我也试过了SharedManager,但它只适用于EVENT_DISPATCH ......

3 个答案:

答案 0 :(得分:1)

除非特定情况,否则最好在onBootstrap注册您的活动。

init用于“早期事件”。

我找到了一个非常明确的链接:http://samsonasik.wordpress.com/2013/03/30/zend-framework-2-getting-closer-with-eventmanager/

您可以在Zend\ModuleManager\Listener\DefaultListenerAggregate::attch中找到默认MVC事件的顺序:

 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;
    }

答案 1 :(得分:0)

请参阅http://akrabat.com/zend-framework-2/module-specific-bootstrapping-in-zf2/

仅找到此解决方案:在onBootstrap()中附加侦听器 - Module.php类中的方法:

...

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'));
}

...

答案 2 :(得分:0)

您最好使用共享管理器。下面的示例用于在我们获得xmlHttpRequest时禁用布局,优先级-95是使事情有效的关键点。

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); 
}