简体   繁体   English

Zf2设置新的配置值

[英]Zf2 Set new config values

I am looking for a possibility to override existing configuration values with new ones during runtime. 我正在寻找一种在运行时替换现有配置值的可能性。

So sth. 等等。 like that would be nice: 那样会很好:

$this->serviceLocator->set('Config', $this->config);

Is there a way to do that? 有没有办法做到这一点?

Yeah, you can do that. 是的,你可以做到。 Whether you should do that is an entire matter altogether. 是否应该这样做完全是一回事。 So: 所以:

$this->serviceLocator->setAllowOverride(true); 
// service keys are case insensitive
// just remember that $this->config should contain the whole config
$this->serviceLocator->setService('Config', $this->config);
$this->serviceLocator->setAllowOverride(false);

For setService to work you need to toggle a flag called allowOverride . 对于setService工作,你需要切换一个叫做标志allowOverride You can see that here . 您可以在这里看到。 Afterwards you should probably disable overrides, hence another call to setAllowOverride . 之后,您可能应该禁用替代,因此需要再次调用setAllowOverride

Also: you can hook into EVENT_MERGE_CONFIG and change it there . 另外: 您可以挂钩EVENT_MERGE_CONFIG并在那里进行更改

Existing config values can simply be overridden by custom setting them inside the config.php file in the config folder of your modules. 通过在模块的config文件夹中的config.php文件中自定义设置,可以简单地覆盖现有的配置值。 You don't need to do this using the ServiceLocator . 您无需使用ServiceLocator进行此操作。 The array in this file is a global array. 该文件中的数组是全局数组。 The keys in the array will be overwritten in the order that you are loading your modules. 数组中的键将按照加载模块的顺序覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM