[英]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.