简体   繁体   中英

Loading Zend Framework 2 Config from Database

I am working on a project which requires me to load configuration values from database

I have a local config file filters.local.php I want to remove it and put the values in database and when the application loads instead of auto loading config values from autoload folder or anyother config file, I want to mergre config from database into zend framework 2 config.

Is there anyway to achieve this?

Use Zend Config Writer .

With Zend Config Writer you can write config files with your values from database.

Example:

  • In your controller

     public function writeFiltersConfigAction(){ $config = new \\Zend\\Config\\Config(array(), true); $config->filters = array(); $citiesArray = array( 1 => Paris, 2=>London, 3=>Berlin); // Of course, these values can be taken from database $config->filters->cities = $citiesArray; $writer = new \\Zend\\Config\\Writer\\PhpArray(); $writer->toFile('config/autoload/filters.local.php',$config); } 
  • When you will access write-filters-config in your browser, you will see that Zend Config Writer will create filters.local.php file with a php array.

  • You can create a cronjob on that action, so that filters.local.php get updated how often you want

  • After that, of course, you can read your config file into your application. You will read filters from the config files, avoiding databases queries every time

     $config = $this->getServiceLocator()->get('Config'); $filters = $config['filters']; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM