1

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?

4

2 回答 2

8

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

$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并更改它

于 2015-05-05T12:32:06.737 回答
0

config.php现有的配置值可以通过在模块的配置文件夹中的文件中自定义设置来简单地覆盖。您不需要使用ServiceLocator. 该文件中的数组是一个全局数组。数组中的键将按照您加载模块的顺序被覆盖。

于 2015-05-05T08:56:23.180 回答