aut*_*tix 2 configuration config configuration-files zend-framework2
在ZF2应用程序中,我有一些共同点:1.需要根据环境的不同而不同; 2.具体针对具体模块.我curently使用它像这里描述:
global.php&local.php
return array(
...
'modules' => array(
'Cache' => array(
'ttl' => 1, // 1 second
)
)
...
);
Run Code Online (Sandbox Code Playgroud)
模块类
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'],
...
));
},
...
)
);
}
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我相信,模块特定设置应该通过模块中的一个中心位置访问 - 类的getConfig()方法Module.像这样:
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'],
...
));
},
...
)
);
}
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
问题是,我没有得到,如何访问模块中的global.php/local.php配置getConfig().我该怎么做?
Sam*_*Sam 17
每个加载的模块的每个配置都将合并为一个配置.即这将是:
$serviceManager->get('config');
Run Code Online (Sandbox Code Playgroud)
背后的原因(global|local).config.php仅仅是出于使用目的.应始终部署全局配置文件.但是,本地配置文件只应部署为可分发的别名local.config.php.dist.
无论它们在哪里,都不会加载分布.然而,ZF2的常见概念是将可分发的副本复制到/config/autoloadZF2应用程序的目录中并将它们重命名为local.config.php
一个例子:
// YourModule/config/module.config.php
return array(
'key' => 1337
);
// YourModule/config/local.yourmodule.php.dist
return array(
'key' => 7331
);
Run Code Online (Sandbox Code Playgroud)
现在,当您发布/部署应用程序时,只会module.config.php使用它.如果有人想要更改模块的配置,他们就永远不会触摸module.config.php,因为当您的模块更新时,这个文件会不断被覆盖.
然而,人们可以做的是复制:
YourModule/config/local.yourmodule.php.dist
to
/config/autoload/local.yourmodule.php
Run Code Online (Sandbox Code Playgroud)
并更改此本地配置中的配置值.
要理解:
希望这更清楚一点
最后:
/config/autoload/mymodule.local.php并覆盖您ttl的开发价值LoadOrder:
最后一个有趣的部分,我已经完全忘记了,将是配置文件的加载顺序.由于所有文件都已合并,因此请务必注意这一点!
/config/application.config.php/modules/{module}/config/module.config.php*/config/autoload/{filename}.phpasterix它实际上不是 module.config.php被调用的,而是Module-classes配置函数.主要是:
getConfig()getServiceConfig()getViewHelperConfig()Zend\ModuleManager\Feature\{feature}ProviderInterface如果我正确地理解了这一部分ConfigListener,那么getConfig()将首先调用并且所有特殊的{feature}ProviderInterfaces将覆盖数据getConfig(),但不要认为这是理所当然的,它需要检查!
| 归档时间: |
|
| 查看次数: |
14058 次 |
| 最近记录: |