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?

blackbishop
  • 30,945
  • 11
  • 55
  • 76
user3314010
  • 41
  • 1
  • 4

2 Answers2

8

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. You can see that here. Afterwards you should probably disable overrides, hence another call to setAllowOverride.

Also: you can hook into EVENT_MERGE_CONFIG and change it there.

guessimtoolate
  • 8,372
  • 2
  • 18
  • 26
  • This worked perfectly for me. It is unfortunate that I have had to add to config at runtime, but the alternative was overriding a tonne of zend classes which is always a pain :) – Stephen Nov 18 '15 at 12:50
0

Existing config values can simply be overridden by custom setting them inside the config.php file in the config folder of your modules. You don't need to do this using the 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.

Wilt
  • 41,477
  • 12
  • 152
  • 203
  • I know that. I want to override the config for example in the onBootstrap-function of module.php – user3314010 May 05 '15 at 09:07
  • 1
    @user3314010 Maybe you can explain what you want to achieve? I think config should be static. If you make config dynamic you can end up in trouble. You can load/set custom config for certain services inside your factories. – Wilt May 05 '15 at 10:50
  • 1
    Have found a solution which works for me: $serviceLocator->setAllowOverride(true); $serviceLocator->setService('config', $configuration); $serviceLocator->setAllowOverride(false); – user3314010 May 05 '15 at 12:30