[英]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
。
通过在模块的config文件夹中的config.php
文件中自定义设置,可以简单地覆盖现有的配置值。 您无需使用ServiceLocator
进行此操作。 该文件中的数组是全局数组。 数组中的键将按照加载模块的顺序覆盖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.