繁体   English   中英

Zf2设置新的配置值

[英]Zf2 Set new config values

我正在寻找一种在运行时替换现有配置值的可能性。

等等。 那样会很好:

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

有没有办法做到这一点?

是的,你可以做到。 是否应该这样做完全是一回事。 所以:

$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);

对于setService工作,你需要切换一个叫做标志allowOverride 您可以在这里看到。 之后,您可能应该禁用替代,因此需要再次调用setAllowOverride

另外: 您可以挂钩EVENT_MERGE_CONFIG并在那里进行更改

通过在模块的config文件夹中的config.php文件中自定义设置,可以简单地覆盖现有的配置值。 您无需使用ServiceLocator进行此操作。 该文件中的数组是全局数组。 数组中的键将按照加载模块的顺序覆盖。

暂无
暂无

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

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