Welcome Guest, Not a member yet? Register   Sign In
Dynamic Configuration from Database Instead of Config File
#1

[eluser]Vheissu[/eluser]
Before I ask my question, I used the search and tried myself to do this, but I guess my Codeigniter-fu just isn't up to scratch just yet. Anyway...

I am developing a modular, themeable open source CMS which is using an SQLite database and the aim is for it to require almost zero configuration except for a site name and the url. I don't want users to have to edit a config file because lets face it, a lot of people are stupid and I work in web development so I probably encounter these people more often than others.

How do I make it so that I can fetch certain config values from my database as opposed to using a config file? I would imagine I would have to create a custom MY_Config to fetch the values from the database, but how would I get CI to look for the site url, site name etc from my database instead?

Please keep in mind that I am using an SQLite 3 database and not a MySQL database. I am also using the latest version of Codeigniter 2 if that helps.
#2

[eluser]WanWizard[/eluser]
I do it the other way around. For ExiteCMS I have default values in the config files, which I overrule with entries from my configuration table:
Code:
// retrieve all configuration items
$query = $this->CI->db->get('configuration');

// loop through the results
foreach ($query->result_array() as $row)
{
    if ( $row['module'] == 'exitecms' )
    {
        // store internal items directly
        $this->CI->config->config['exitecms'][$row['name']] = $row['value'];

        // config values that need to be copied to the CI config
        switch($row['name'])
        {
            case 'locale':
                // set the CI language to the locale defined in the configuration
                $this->CI->config->config['language'] = $row['value'];
                break;
            case 'login_expire':
                // set the CI session timeout to the login expire value defined in the configuration
                $this->CI->config->config['sess_expiration'] = $row['value'];
                // do we require session expiring when the browser closes?
                if ( $row['value'] == 0 )
                {
                    $this->CI->config->config['sess_expire_on_close'] = TRUE;
                }
                break;
            case 'country':
            case 'timezone':
            case 'url_suffix':
            case 'encryption_key':
                // copy these values to their CI equivalent
                $this->CI->config->config[$row['name']] = $row['value'];
                break;
            case 'compress_output':
                // the output library requires a boolean here
                $this->CI->config->config[$row['name']] = (bool) $row['value'];
                break;
            default:
                // skip the rest
                break;
        }

    }
    else
    {
        // store module items in a module per array
        if ( ! isset($this->CI->config->config['exitecms'][$row['module']]) )
        {
            $this->CI->config->config['exitecms'][$row['module']] = array();
        }
        $this->CI->config->config['exitecms'][$row['module']][$row['name']] = $row['value'];
    }
}
#3

[eluser]slowgary[/eluser]
You may be able to just set $base_url from the config item manually after you fetch it from the database. Give it a whirl.
#4

[eluser]Vheissu[/eluser]
Wow WanWizard, awesome. I presume I just place that above code or equivalent inside of MY_Controller. Upon reading the CI Config class it mentions being able to set config items and overwriting existing ones inside of your config file (http://ellislab.com/codeigniter/user-guide/libraries/config.html).

I wonder if the below code would work just as well and with less code?

Code:
$settings = $this->db->get('settings');
    
    // loop through the results
    foreach ($settings->result_array() as $row)
    {
        switch($row['setting_name']) {

            case 'site_url':
                $this->config->set_item('site_url', $row['setting_value']);
            break;

            case 'site_name':
                $this->config->set_item('site_name', $row['setting_value']);
            break;

        }
    }

That above code based on your answer should work as I am not storing values in my settings table as arrays and just name/value pairs instead for simplicity. Thank you for your answers and your code example WanWizard. Side note: I wonder why functionality for storing configuration values inside of a database doesn't exist out-of-the-box in Codeigniter? Perhaps a modification to the Codeigniter config class to allow you to specify a table name for configuration settings.
#5

[eluser]WanWizard[/eluser]
In my case it's in a core module controller, but a MY_Controller works just as well.

Offcourse set_item() works fine, I just modify the array directly to avoid the method calls. And I've got lots of code (above is just a snippet), because I have to define over 60 config settings, to be able to override most of CI's config functionality, or to add ExiteCMS specific config settings.
#6

[eluser]Vheissu[/eluser]
[quote author="WanWizard" date="1286420778"]In my case it's in a core module controller, but a MY_Controller works just as well.

Offcourse set_item() works fine, I just modify the array directly to avoid the method calls. And I've got lots of code (above is just a snippet), because I have to define over 60 config settings, to be able to override most of CI's config functionality, or to add ExiteCMS specific config settings.[/quote]

I've taken looks at different pieces of ExciteCMS here and there ever since I discovered it a while back and all I can say is, wow. Thanks for helping me out. I'm sure others would probably find this post useful as well, seems like functionality most people developing a site especially for end-users would prefer to do.




Theme © iAndrew 2016 - Forum software by © MyBB