Loading modules dynamically in Zend Framework 2

I also asked this question yesterday, but this code contains code.

Question

My application has multiple modules and 2 types of user accounts. Some modules are loaded always

that are present in application.config.php

, some of them conditional

, that is, some are loaded for the user type A

and some for the usertype B

After looking at the documentation and questions about Stack Overflow, I understand some of the functionality of the ModuleManager and started to implement the logic that although I can work.

Some, as I found out a way to load modules that are not present in application.config.php

[SUCCESS] , but their configuration does not work [RELEASE] , ie. if in the method onBootstrap

i get the service ModuleManager

and do getLoadedModules()

i get the list of all loaded modules. Subsequently, if I try to get some service from this dynamically loadable module, it throws an exception.

Zend \ ServiceManager \ ServiceManager :: get failed to get or instantiate for jobs_mapper

Note that factories and all other things work fine because if I load the module from application.config.php it works fine

Likewise, when I try to access any route from a dynamically loaded module, it throws 404 Not Found

which made it clear that the configuration from the module.config.php of those modules is not loaded even if the module is loaded by the ModuleManager.

Code

In the Module.php of my application module, I have injected InitProviderInterface

and added a method init(ModuleManager $moduleManager)

where I catch the connectManager loadModules.post module and load the modules

public function init(\Zend\ModuleManager\ModuleManagerInterface $moduleManager)
{
    $eventManager = $moduleManager->getEventManager();
    $eventManager->attach(\Zend\ModuleManager\ModuleEvent::EVENT_LOAD_MODULES_POST, [$this, 'onLoadModulesPost']);
}

      

Then in the same class, process the method onLoadModulesPost

and start loading my dynamic modules

public function onLoadModulesPost(\Zend\ModuleManager\ModuleEvent $event)
{

    /* @var $serviceManager \Zend\ServiceManager\ServiceManager */
    $serviceManager = $event->getParam('ServiceManager');
    $configListener = $event->getConfigListener();

    $authentication = $serviceManager->get('zfcuser_auth_service');

    if ($authentication->getIdentity())
    {
        $moduleManager = $event->getTarget();

        ...
        ...

        $loadedModules = $moduleManager->getModules();
        $configListener = $event->getConfigListener();
        $configuration = $configListener->getMergedConfig(false);

        $modules = $modulesMapper->findAll(['is_agency' => 1, 'is_active' => 1]);

        foreach ($modules as $module)
        {
            if (!array_key_exists($module['module_name'], $loadedModules))
            {
                $loadedModule = $moduleManager->loadModule($module['module_name']);

                //Add modules to the modules array from ModuleManager.php
                $loadedModules[] = $module['module_name'];

                //Get the loaded module
                $module = $moduleManager->getModule($module['module_name']);

                //If module is loaded succesfully, merge the configs
                if (($loadedModule instanceof ConfigProviderInterface) || (is_callable([$loadedModule, 'getConfig'])))
                {
                    $moduleConfig = $module->getConfig();
                    $configuration = ArrayUtils::merge($configuration, $moduleConfig);
                }
            }
        }

        $moduleManager->setModules($loadedModules);
        $configListener->setMergedConfig($configuration);
        $event->setConfigListener($configListener);
    }
}

      

Questions

  • Is it possible to achieve what I am trying to do?
  • If so, what's the best way?
  • What am I missing in my code?
+3


source to share


2 answers


I think there is some fundamental mistake in what you are trying to do here: you are trying to load modules based on the merged configuration and hence create a circular dependency between the modules and the merged config.

I would suggest doing this.

Instead, if you have logic that determines which part of the application should be loaded, put it in config/application.config.php

that is responsible for getting the list of modules.



At this point, however, it is too early to depend on any service because the definition of the service also depends on the combined configuration.

Another thing to clarify is that you are trying to make these decisions based on whether the authenticated user (request information, not environment information) meets certain criteria, and then modify the whole application based on that.

Don't do this: instead, move the solution to the component to be conditionally enabled / disabled by arming it.

+3


source


What you ask can be done, but that does not mean that you should.

It is difficult to come up with a suitable solution without knowing the complexity of the application you are building.

Using guards will certainly help decouple your code, but using it alone doesn't mean scalability and maintainability if that's a concern?



I would suggest using token based authentication. Instead of maintaining validation logic in every application, write your validation logic in one common place so that every request can use that logic regardless of the application. Choosing a reverse proxy (Nginx) to support your validation logic (using Lua) gives you the flexibility to design your application in any language.

Moreover, validating credentials at the load balancing layer substantially eliminates the need for session state, you can have many separate servers running across multiple platforms and domains, reusing the same token to authenticate a user.

User identification, account type, and loading of different modules becomes a trivial task. By simply passing information about tokens through an environment variable, it can be read in your file config/application.config.php

without having to access the database, cache, or other services first.

0


source







All Articles