我想知道是否有任何方法手动加载模块。它的意思是,假设我有一个模块叫Application
,另一个模块叫Clients
。我不希望Clients
模块进入application.config.php
文件,并保持自动加载,而不管应用程序的首选项。我应该手动加载它从我的第一个模块名为Application
。它也可以从任何自定义位置,而不是'module'目录。
欢迎任何得体的回答。谢谢极客们。
不,Zend Framework 2不允许你从另一个模块加载模块。这是特别没有提供,以防止不必要的副作用。您必须指定两件事:
- 模块的加载路径;当你加载一个模块
- 你想要加载的模块;哪些模块是启用的
这是实现所需需求的变通方法。我把它贴在这里,这样任何发现这个问题的人都可以从中受益。
这个概念实际上是传递经过验证的模块以及由自定义类处理的主配置。这样它就会被zend的本地模块管理器自动加载。
1。首先在主配置文件中添加模块的新路径。
<?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。在主模块中创建一个类函数,检查哪些模块是启用的,并获取列表
<?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。修改public
目录下的index.php
文件,用新加载的模块列表注入修改后的配置。
//
// 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;
}
以上只是我的概念的一个抽象,我实现和工作。我不能张贴完整的代码,所以只从上面得到的概念。