Reputation: 51
I have following 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'
)
)
);
and in the Application/Module.php I have (few of the functions):
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'));
},
),
);
}
what I'm trying to achieve here is to have modules dynamically loaded based on a setting from database.
i get no error in loading modules ... when i try calling back $moduleManager->getLoadedModules(); i see that the module is in the loaded list but its config and its functionality doesnt work. Specifically i have routes in that module and when trying to access them i get 404. but if i include the module in the application.config all works perfect.
Possible to achieve? If yes any guidelines?
Thanks
UPDATE
I managed to get the modules dynamically loaded within the Module::init() method ... but without any success accessing the ServiceManager and/or db access to load the list of modules from db ...
Upvotes: 2
Views: 1026
Reputation: 359
This is an old question but I saw it today trying to do the exact same thing. My code is geared toward ZF3 but should work on ZF2:
https://github.com/basicinvoices/basicinvoices-modulemanager
The basics I've followed...
ModuleManager::loadModule()
method. We also add it to the modules array and set it back to the ModuleManager
...and that's about it.
Upvotes: 0