Zend framework2 动态加载ZF2模块
我有以下application.configZend framework2 动态加载ZF2模块,zend-framework2,zend-framework-mvc,zend-framework-modules,Zend Framework2,Zend Framework Mvc,Zend Framework Modules,我有以下application.config return array( 'modules' => array( 'Application', 'ErrorHandler' ), 'module_listener_options' => array( 'module_paths' => array( './module', './vendor' ), 'config_glob_paths' => a
return array(
'modules' => array(
'Application',
'ErrorHandler'
),
'module_listener_options' => array(
'module_paths' => array(
'./module',
'./vendor'
),
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php'
)
)
);
在Application/Module.php中,我有(几个函数):
我在这里试图实现的是根据数据库中的设置动态加载模块
我在加载模块时没有错误。。。当我尝试回调$moduleManager->getLoadedModules()时;我看到模块在已加载列表中,但其配置和功能不起作用。具体来说,我在该模块中有路由,当尝试访问它们时,我得到404。但是,如果我在application.config中包含该模块,那么所有的工作都会非常完美
可能实现?如果有,有什么指导方针吗
谢谢
更新
我设法在Module::init()方法中动态加载模块。。。但是没有任何成功访问ServiceManager和/或db access以从db加载模块列表 这是一个老生常谈的问题,但我今天看到它试图做完全相同的事情。我的代码面向ZF3,但应适用于ZF2: 我遵循的基本原则
ModuleManager::loadModule()
方法加载它们。我们还将其添加到模块数组中,并将其设置回ModuleManager
…仅此而已。看一看。操作合并的配置部分可以帮助您解决问题我安装了一个小软件包,为模块加载添加了条件,我还没有完成:SL在技术上可以在服务中使用,因此我可以将其推入条件计算器而不是闭包中,让您访问DB。这方面有什么进展吗?在您的步骤3中,我们如何在zend
config
服务中合并Modulesmodule.config.php
中的配置值呢!
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$this->initModules($e);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
private function getModules(MvcEvent $e) {
$sm = $e->getApplication()->getServiceManager();
$moduleTable = $sm->get('ModuleTable');
$modules = array();
foreach ($moduleTable->fetchAll() as $m) {
$modules[] = $m;
}
return $modules;
}
private function initModules(MvcEvent $e) {
$modules = $this->getModules($e);
$serviceManager = $e->getApplication()->getServiceManager();
$moduleManager = $serviceManager->get('ModuleManager');
$loadedModules = $moduleManager->getLoadedModules();
foreach ($loadedModules as $module) {
$this->loadedModules[] = str_replace('\Module', '', get_class($module));
}
foreach ($modules as $module) {
try {
$moduleManager->loadModule($module->getName());
$this->loadedModules[] = $module->getName();
} catch (\Exception $e) {
$this->failedModules[] = $module->getName();
}
}
if (count($this->failedModules) > 0) {
// Error in loading modules
exit;
}
return $this;
}
public function getServiceConfig()
{
return array(
'factories' => array(
'ModuleTable' => function($sm) {
return new ModuleTable($sm->get('Zend\Db\Adapter\Adapter'));
},
),
);
}