我想创建一个服务,帮助用户从系统管理面板动态加载模块。这是我的代码。
if(!$this->isModuleInstalled($moduleName)) {
$appConfigService = $this->getServiceManager()->get('ApplicationConfig');
$appConfig = new Config($appConfigService, true);
$modules = $config->modules->toArray();
end($modules);
$nextModuleKey = (key($modules) + 1);
unset($modules);
$config->modules->{$nextModuleKey} = $moduleName;
$writter = new Writer\PhpArray();
$writter->toFile(
__DIR__ . '/../../../../../config/application.config.php',
$config->toArray()
);
}
我认为这不是动态加载模块的最佳实践。
试试这个
$configuration = $serviceManager->get('ApplicationConfig');
$configuration['modules'][] = 'ModuleName';
$serviceManager->setService('ApplicationConfig', $configuration);
$serviceManager->get('ModuleManager')->loadModules();