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?
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?
是的,你可以这样做。你是否应该这样做完全是一回事。所以:
$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.php
现有的配置值可以通过在模块的配置文件夹中的文件中自定义设置来简单地覆盖。您不需要使用ServiceLocator
. 该文件中的数组是一个全局数组。数组中的键将按照您加载模块的顺序被覆盖。