How do you load config settings from the database

2019-03-08 12:06发布

问题:

I have a number of settings that are currently in the config.yml file.

Going forward I want to be able to develop an interface where administrators will be able to update these settings, so I want to be able to manage these settings through the database.

How would I be able to load these settings from the database into Symfony2 and where and when would I load them?

Cheers

Adam

回答1:

There's a cookbook article that explains roughly how to do this (albeit briefly), in reference to loading in settings externally from Drupal. The basic idea is to do something like this in your config (example is yml):

# app/config/config.yml
imports:
    - { resource: parameters.php }

then in parameters.php you can do whatever you need to to get your config, and set it as follows:

$container->setParameter('my.db.parameter', $value);

(taken from the cookbook, slightly modified).



回答2:

Take a look at the UnifikDatabaseConfigBundle. It creates a database structure that enable configuration of Symfony parameters straight from the database.