简体   繁体   中英

Load zend config parameters from database

In one of my project(Using Zend framework), I am currently retrieving the LDAP configuration from the ini file. My client wants to change it to a database configuration.

ie, i want to read the LDAP configs from DB and then bind it to other resource params in the ini file. Is there any way to do this?

In my local.ini file, i have following configs

ldap.server.host = "host"
ldap.server.username = "user"
ldap.server.password = "pwd"
ldap.server.baseDn = "DC=comp,DC=com"
ldap.server.accountDomainName = "abc.intra"
ldap.server.accountDomainNameShort = "dell"

in my bootstrap,

protected function _initLocalConfig()
    {
        $globalConfig = new Zend_Config($this->getOptions(), true);
        try {
            $localConfig = new Zend_Config_Ini(APPLICATION_PATH . '/configs/local.ini');
            $globalConfig->merge($localConfig);
            $this->setOptions($globalConfig->toArray());
        } catch (Zend_Config_Exception $e) {
            throw new Exception('File /configs/local.ini not found. Create it, it can be empty.');
        }
    }  

Can someone tell me how can i retrieve the above mentioned LDAP params from DB and then bind it to the Zend config params.

The general algorithm should be like that:

  1. You boot general config

  2. After that you bootstrap db initialisation

  3. Then you init you ldap configuration and merge it with config

So I'd added next methods to bootstrap.php

protected function _initConfig() 
{
    $config = new Zend_Config($this->getApplication()->getOptions(), true);
    Zend_Registry::set('config', $config);
}

protected function _initLDAPConfig()
{
    $this->bootstrap('config');
    $this->bootstrap('db');

    $globalConfig = Zend_Registry::get('config');
    try {
        /**
         * Do a database query to get your ldap config from database
         * and convert it to array
         */
         $globalConfig->merge(new Zend_Config($ldapConfig));
    } catch (Zend_Config_Exception $e) {
        throw new Exception('Failed bootstraping LDAP config');
    }
}

Note: I always have my config in Zend_Registry, so you should adjust my example to your needs.

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