2

当我跟踪zf2中的代码时,我无法找到应用程序服务注册的地方。 这里是application.phpzf2中的loadModules.post是一个函数吗?

public static function init($configuration = array()) 
{ 
    $smConfig = isset($configuration['service_manager']) ? $configuration['service_manager'] : array(); 
    $serviceManager = new ServiceManager(new Service\ServiceManagerConfig($smConfig)); 
    $serviceManager->setService('ApplicationConfig', $configuration); 
    $serviceManager->get('ModuleManager')->loadModules(); 
    return $serviceManager->get('Application')->bootstrap(); 
} 

代码 “$ serviceManager-> GET( '应用')” 的代码是让应用程序服务。但是应用服务注册在哪里?

然后我发现Application服务与ZF2/library中的代码行“$ this-> getEventManager() - >触发器(ModuleEvent :: EVENT_LOAD_MODULES_POST,$ this,$ this-> getEvent())”相关/Zend/MoudleManager/MoudleManager.php

public function loadModules() 
{ 
    if (true === $this->modulesAreLoaded) { 
     return $this; 
    } 

    $this->getEventManager()->trigger(ModuleEvent::EVENT_LOAD_MODULES, $this, $this->getEvent()); 

    /** 
    * Having a dedicated .post event abstracts the complexity of priorities from the user. 
    * Users can attach to the .post event and be sure that important 
    * things like config merging are complete without having to worry if 
    * they set a low enough priority. 
    */ 
    $this->getEventManager()->trigger(ModuleEvent::EVENT_LOAD_MODULES_POST, $this, $this->getEvent()); 

    return $this; 
} 

的另一个问题是,“ModuleEvent :: EVENT_LOAD_MODULES_POST,这是触发功能的loadModules.post.The第一个参数是一个函数名,所以是loadModules.post一个函数?在哪里定义的?

在此先感谢。

回答

3

我会先解决最后一个问题。触发方法的第一个参数是而不是一个函数,它只是一个名字;按照惯例,该名称通常反映了它被触发的方法,可选地带有后缀以提供更多上下文(例如“.pre”,“.post”等)。

“loadModules.post”是一旦所有模块都已加载,则由ModuleManager::loadModules()触发的事件。当一个事件被触发时,那个事件的任何监听器都会被提供的参数触发。事件对象本身也会有一个“目标”,在这种情况下它将是ModuleManager。

关于“应用程序”服务,您必须查看MVC图层的内部。 大多数MVC服务在Zend\Mvc\Service\ServiceListenerFactory中定义。如果您查看该课程,您会看到它指定Application以使用Zend\Mvc\Service\ApplicationFactory来创建应用程序实例。 ServiceListenerFactory作为创建ModuleManager的工厂的一部分进行检索。它有点间接,但关系是由操作顺序和对象之间的关系来定义的。

+0

谢谢马修,我明白了。并感谢yechabbi。 – Leo

1

创建ServiceListener实例时,大部分Zend \ ServiceManager \ ServiceManager注册服务都由ServiceListener工厂Zend\Mvc\Service\ServiceListenerFactory配置。

默认配置实际上存储在ServiceListenerFactory::defaultServiceConfig。 (如果你通过代码,你会看到“申请”的别名被定义有)

对于你的第二个问题,在ModuleEvent :: EVENT_LOAD_MODULES仅仅是被用来识别的ModuleEvent常量之一不同的模块加载事件。

实际上在应用程序的不同阶段触发了四个模块事件。我还没有机会使用这些事件,并且我认为它们主要由框架本身使用。到目前为止,我只有机会使用Zend \ Mvc \ MvcEvent相关事件。

相关问题