当我跟踪 zf2 中的代码时,我找不到应用程序服务注册的位置。 这是application.php中的代码
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')”用于获取Application服务。但是应用程序服务在哪里注册的呢?
然后我发现应用程序服务与ZF2/library/中的代码行“$this->getEventManager()->trigger(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。触发函数的第一个参数是函数名称。那么loadModules.post是一个函数吗?它在哪里定义的?
提前致谢。
最佳答案
我先解决最后一个问题。触发方法的第一个参数不是一个函数,它只是一个名称;按照惯例,该名称通常反射(reflect)触发它的方法,可以选择使用后缀来提供更多上下文(例如“.pre”、“.post”等)。
“loadModules.post”是由 ModuleManager::loadModules()
触发的事件一旦所有模块都已加载。触发事件时,将使用提供的参数触发该事件的任何监听器。事件对象本身也将有一个“目标”,在本例中为 ModuleManager。
关于“应用程序”服务,您必须了解MVC层的内部结构。
大多数 MVC 服务在 Zend\Mvc\Service\ServiceListenerFactory
中定义。 。如果您查看该类,您会发现它分配了 Application
使用Zend\Mvc\Service\ApplicationFactory
创建一个应用程序实例。 ServiceListenerFactory
作为创建 ModuleManager 的工厂的一部分被检索。这有点间接,但关系是由操作顺序和对象之间的关系定义的。
关于zend-framework2 - zf2中的loadModules.post是一个函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14382011/