Configuration 如何在Zend Framework 2应用程序的模块的getConfig()中使用global.php/local.php配置?
在ZF2应用程序中,我有一些COFIG,即:1。需要根据环境的不同而有所不同;2.特定于具体模块。我现在正按照描述的方式使用它: global.php和local.phpConfiguration 如何在Zend Framework 2应用程序的模块的getConfig()中使用global.php/local.php配置?,configuration,zend-framework2,config,configuration-files,Configuration,Zend Framework2,Config,Configuration Files,在ZF2应用程序中,我有一些COFIG,即:1。需要根据环境的不同而有所不同;2.特定于具体模块。我现在正按照描述的方式使用它: global.php和local.php return array( ... 'modules' => array( 'Cache' => array( 'ttl' => 1, // 1 second ) ) ... ); 模块类 Module { ..
return array(
...
'modules' => array(
'Cache' => array(
'ttl' => 1, // 1 second
)
)
...
);
模块类
Module {
...
public function getServiceConfig() {
try {
return array (
'factories' => array(
'Zend\Cache\Adapter\MemcachedOptions' => function ($serviceManager) {
return new MemcachedOptions(array(
'ttl' => $this->getConfig()['modules']['Cache']['ttl'],
...
));
},
...
)
);
}
...
}
...
}
它工作得很好,但我认为,应该通过模块中的一个中心位置访问特定于模块的设置--module
类的getConfig()
方法。像这样:
class Module {
public function getConfig() {
$moduleConfig = include __DIR__ . '/config/module.config.php';
$application = $this->getApplicationSomehow(); // <-- how?
$applicationModuleConfig = $application->getConfig()['modules'][__NAMESPACE__];
$config = array_merge($moduleConfig, $applicationModuleConfig);
return $config;
}
...
public function getServiceConfig() {
try {
return array (
'factories' => array(
'Zend\Cache\Adapter\MemcachedOptions' => function ($serviceManager) {
return new MemcachedOptions(array(
'ttl' => $serviceManager->get('Config')['modules']['Cache']['ttl'],
...
));
},
...
)
);
}
...
}
...
}
类模块{
公共函数getConfig(){
$moduleConfig=include _DIR__.'/config/module.config.php';
$application=$this->getApplicationSomehow();//getConfig()['modules'][\uuuu名称空间];
$config=array\u merge($moduleConfig$applicationModuleConfig);
返回$config;
}
...
公共函数getServiceConfig(){
试一试{
返回数组(
“工厂”=>数组(
'Zend\Cache\Adapter\MemcachedOptions'=>函数($serviceManager){
返回新的MemcachedOptions(数组(
'ttl'=>$serviceManager->get('Config')['modules']['Cache']['ttl'],
...
));
},
...
)
);
}
...
}
...
}
问题是,我不知道如何访问模块的
getConfig()
中的global.php/local.php配置。如何操作?每个加载模块的每个配置都将合并到一个配置中。即:
$serviceManager->get('config');
(global | local).config.php
背后的原因仅仅是出于使用目的。应始终部署全局配置文件。但是,本地配置文件应仅作为分发文件部署,别名为Local.config.php.dist
配送将不会加载,无论它们位于何处。然而,ZF2的常见概念是将分发文件复制到ZF2应用程序的/config/autoload
-目录中,并将其重命名为local.config.php
一个例子:
// YourModule/config/module.config.php
return array(
'key' => 1337
);
// YourModule/config/local.yourmodule.php.dist
return array(
'key' => 7331
);
现在,在发布/部署应用程序时,只会使用module.config.php
。如果有人想要更改您模块的配置,他们将永远不会触摸Module.config.php
,因为当您的模块更新时,此文件将不断被覆盖
然而,人们可以做的是复制:
YourModule/config/local.yourmodule.php.dist
to
/config/autoload/local.yourmodule.php
并更改此本地配置中的配置值
理解:
// YourModule/config/module.config.php
return array(
'key' => 1337
);
// YourModule/config/local.yourmodule.php.dist
return array(
'key' => 7331
);
- 您应该始终为实时场景配置尽可能好的模块
- 如果您有特定于环境的需要,请使用本地配置覆盖此配置
- 本地配置永远不会自动部署,这是一项手动任务,需要从环境本身内部完成
// YourModule/config/module.config.php
return array(
'key' => 1337
);
// YourModule/config/local.yourmodule.php.dist
return array(
'key' => 7331
);
- 为实时场景配置模块
- 在开发机器上创建一个
,并用它的开发值覆盖/config/autoload/mymodule.local.php
ttl
// YourModule/config/module.config.php
return array(
'key' => 1337
);
// YourModule/config/local.yourmodule.php.dist
return array(
'key' => 7331
);
最后一个有趣的部分,我已经完全忘记了,是配置文件的加载顺序。由于所有文件都被合并,这一点很重要
- 首先加载的是
/config/application.config.php
- 第二个要加载的是每个模块
*/Modules/{module}/config/module.config.php
- 最后但并非最不重要的是,将加载可自动加载的文件
/config/autoload/{filename}.php
模块
-类配置函数。主要是:
getConfig()
getServiceConfig()
getViewHelperConfig()
- 最终,一切都在
如果我理解正确,那么将首先调用
getConfig()
,所有特殊许可的{feature}providerInterface
将覆盖getConfig()
的数据,但不要想当然,这需要检查 您不应该访问模块#getConfig()
中的其他模块设置。如果依赖其他配置,则只能用于服务目的。因此,您将依赖于模块#getServiceConfig()
,在工厂内,您确实可以访问服务管理器
,并使用$ServiceManager->get('config')访问您的配置代码>。(见山姆的)
默认情况下,配置的加载顺序为:
/config/application.config.php
,即初始配置文件;不适用于模块配置;下面是要加载的配置文件的文件名模式('config\u glob\u path'=>array('config/autoload/{,*.}{global,local}.php')
)
{ModuleNamespace}\Module#getConfig()
(例如Cache\Module#getConfig()
),按照惯例应该加载其/Module/{ModuleNamespace}/config/Module.config.php
李>
/config/autoload/global.php
,不应包含任何特定于模块的配置(见下文)李>
包含特定于环境的设置的/config/autoload/local.php也不应包含任何特定于模块的配置(见下文);不应对其进行版本控制/部署李>
/config/autoload/{ModuleNamespaceLowerCased}.local.php
(例如cache.local.php
),它只包含模块和环境特定的设置以及sho