Muneer
Muneer

Reputation: 7564

Manually load ZF2 modules from a custom location

I wonder if there is any way to load modules manually. It means, say I have a module named Application and another module named Clients. I do not want the Clients module goes into the application.config.php file and keep loading automatically regardless of application preference. I should manually load it from within my first module named Application. It can also be from any custom location other than 'module' directory.

Welcome any decent answers. Thank you geeks.

Upvotes: 1

Views: 705

Answers (2)

Muneer
Muneer

Reputation: 7564

This is a workaround to achieve the desired requirements. I post it here so that any one find this question can get benefit out of it.

The concept is actually to pass the verified modules along with the main configuration processed by a custom class. So that it will be automatically loaded by the native module manager of zend.

1.First add the new path of modules to main configuration file.

<?php return array( // other options 'module_listener_options' => array( 'module_paths' => array( './module', './vendor', './my_module_path', // << HERE WE ADD THE DESIRED MODULE PATH ), // ....... );

2.Create a class function in our main module to check which of the modules are enabled and get the list

<?php namespace Application; class MyModuleManager { public static function AddCustomModules(array $modules) { // find and create the available module array $custom_modules; return array_merge ($modules, $custom_modules); } }

3.Alter index.php file located at public directory to inject the modified configuration with the newly loaded module list.

//
// Setup autoloading
require 'init_autoloader.php';

// Run the application!
Zend\Mvc\Application::init(_my_get_configs(require 'config/application.config.php'))->run();

// Custom function to process custom modules
function _my_get_configs(array $config) {
    if (class_exists ('Application\MyModuleManager')) {
        $module_merged = \Application\MyModuleManager::AddCustomModules($confi['modules']);
        $config['modules'] = $module_merged;
    }
    return $config;
}

The above are just an abstraction of my concept which I implemented and working. I can not post the full code, so get only the concept from above. If any one can improve the above architecture, please suggest.

Upvotes: 0

Jurian Sluiman
Jurian Sluiman

Reputation: 13558

No, Zend Framework 2 does not allow you to load modules from another module. This is specifically not provided to prevent unwanted side effects. There are two things you have to specify:

  1. The module's loading path; where to look when you load a module
  2. The modules you want to load; which modules you enlist are enabled

You cannot have a module which is not listed in the application config but loaded anyhow.

Upvotes: 1

Related Questions