如何加载所有模块的命名空间(整个应用程序的全局自动加载)
到目前为止,我需要将其添加到每个模块中:
public function getAutoloaderConfig()
{
return array(
/* ... */
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
/* ... */
'MyNameSpace' => __DIR__ . '/../../library/MyNameSpace',
),
),
);
}
如何在application.config.php
中实现此功能? (我只想为整个应用程序加载一些基类)
答案 0 :(得分:0)
要加载模块,您需要使用文件/config/application.config.php 将模块列表添加到模块密钥下的数组中。
return array(
// This should be an array of module namespaces used in the application.
'modules' => array(
'Application',
'Pages',
'ModuleName',
),
// ..... rest of array
)
函数getAutoloaderConfig()和getConfig()用于加载模块的配置(模块内部配置)。
http://framework.zend.com/manual/2.0/en/user-guide/modules.html
答案 1 :(得分:0)
在index.php中自动加载该路径,即
set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/../../library/MyNameSpace');
spl_autoload_register(function ($class) {
if(!class_exists($class)) {
$class = str_replace('\\', '/', $class) . '.php';
require_once($class);
}
});
答案 2 :(得分:0)
我认为您不能(或应该)在application.config.php
文件中执行此操作。 ZF2框架中模块背后的重点是将逻辑分离为可重用的独立代码。因此,每个模块都维护和管理自己的配置,包括自动加载,独立于主应用程序或其他模块。这些设置通常可以使用主.global.php
目录的.local.php
和autoload/
文件覆盖,但自动加载应留给每个单独的模块。
如果您尝试包含的模块是您开发的自定义代码段,那么您可能想要查看它们是否实际上是模块,或者仅仅是应用程序模块中的代码。