Zend framework2 ZF2-包括(自动加载)所有模块的类/命名空间

Zend framework2 ZF2-包括(自动加载)所有模块的类/命名空间,zend-framework2,Zend Framework2,如何为所有模块加载命名空间(整个应用程序的全局自动加载) 到目前为止,我需要在每个模块中添加以下内容: public function getAutoloaderConfig() { return array( /* ... */ 'Zend\Loader\StandardAutoloader' => array( 'namespaces' => array(

如何为所有模块加载命名空间(整个应用程序的全局自动加载)

到目前为止,我需要在每个模块中添加以下内容:

 public function getAutoloaderConfig()
    {
        return array(
            /* ... */
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    /* ... */
                    'MyNameSpace'        => __DIR__ . '/../../library/MyNameSpace',            
                ),

            ),
        );
    }

如何在
application.config.php
中实现此功能?(我只想为整个应用程序加载一些基类)

要加载所需的模块,请使用文件/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()用于加载模块的配置(模块内部配置)


在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);
}
});

我认为您不能(或不应该)在
application.config.php
文件中这样做。ZF2框架中模块背后的全部要点是将逻辑分离为可重用、独立的代码位。因此,每个模块独立于主应用程序或其他模块维护和管理自己的配置,包括自动加载。这些设置通常可以用主
自动加载
目录的
.global.php
.local.php
文件覆盖,但是自动加载应该留给每个模块


如果您试图包含的模块是您开发的自定义代码段,那么您可能希望查看它们是否实际上是模块,或者只是属于应用程序模块的代码。

在zf2中,您需要通知应用程序您正在使用的模块。请查收