Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Zend framework2 动态加载ZF2模块_Zend Framework2_Zend Framework Mvc_Zend Framework Modules - Fatal编程技术网

Zend framework2 动态加载ZF2模块

Zend 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

我有以下application.config

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:

我遵循的基本原则

  • 等待模块加载完毕(ModuleEvent::EVENT\u LOAD\u modules\u POST),这样我们就可以访问数据库配置。我以高优先级(9000)连接到它,以确保它在其他事件之前运行
  • 此时,我加载了一个数据库适配器。模块服务尚未分配,因此我们必须手动创建,但这很容易。我们在数据库中搜索尚未加载的活动模块,并使用
    ModuleManager::loadModule()
    方法加载它们。我们还将其添加到模块数组中,并将其设置回
    ModuleManager
  • 配置没有被合并,在某一点上,如果配置已经被缓存,那么如果模块在数据库中发生了更改,我们就会遇到问题。。。但这需要额外的步骤,我们应该检查模块是否有配置,如果有,我们将自己将其合并到合并的配置中

  • …仅此而已。

    看一看。操作合并的配置部分可以帮助您解决问题我安装了一个小软件包,为模块加载添加了条件,我还没有完成:SL在技术上可以在服务中使用,因此我可以将其推入条件计算器而不是闭包中,让您访问DB。这方面有什么进展吗?在您的步骤3中,我们如何在zend
    config
    服务中合并Modules
    module.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'));
                },
            ),
        );
    }