CodeIgniter Forums

Full Version: Custom Global properties (variables)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

Hey, I want to have properties (or variables) globally accessible throughout every class I use in the CI framework. Basically, I get my options from the database (locale, timezone, number of results per page for pagination) and I want them available in models, controllers and function (using global $var). i do not want to use a config file since it's stored in the database and I do some processing on those settings before having them available to the developer. Also, if possible, to have access to them very early in the script execution.

Am I missing something in the docs that can help me? If not, how can it be done efficiently?


El Forum

for this create a library name what every you like, libraries can be access any where in CI, in controller, helpers, model, you can use variables as global variable and can populate through database.

El Forum

I just load them from my configuration table and add them to the config class, so I can access them the CI standard way, as if they were loaded from a config file.

* loads the configuration from the configuration table
* @param void
* @return void
function load_config()
    // retrieve all configuration items
    $query = $this->db->get('configuration');

    // loop through the results
    foreach ($query->result_array() as $row)
        if ( $row['config_module'] == 'exitecms' )
            // store internal items directly
            $this->config->config['exitecms'][$row['config_name']] = $row['config_value'];

            // config values that need to be copied to the CI config
                case 'locale':
                    // set the CI language to the locale defined in the configuration
                    $this->config->config['language'] = $row['config_value'];
                case 'login_expire':
                    // set the CI session timeout to the login expire value defined in the configuration
                    $this->config->config['sess_expiration'] = $row['config_value'];
                case 'log_threshold':
                    // do we need to enable xdebug?
                    if ( $row['config_value'] == 5 && function_exists('xdebug_start_trace') )
                        $row['config_value'] = 4;

                    // we need to inform the Log library of a modified threshold
                    if ( $row['config_value'] != $this->config->config[$row['config_name']] )
                        // need the fetch the object, the class is not available via the superobject
                        $LOG =& load_class('Log');
                        // logging disabled?
                        if ( $row['config_value'] == 0 )
                            $LOG->_enabled = FALSE;
                            $LOG->_threshold = $row['config_value'];
                case 'country':
                case 'timezone':
                case 'url_suffix':
                case 'encryption_key':
                case 'uri_protocol':
                    // copy these values to their CI equivalent
                    $this->config->config[$row['config_name']] = $row['config_value'];
                case 'compress_output':
                    // the output library requires a boolean here
                    $this->config->config[$row['config_name']] = (bool) $row['config_value'];
                    // skip the rest

            // store module items in a module per array
            if ( ! isset($this->config->config['exitecms'][$row['config_module']]) )
                $this->config->config['exitecms'][$row['config_module']] = array();
            $this->config->config['exitecms'][$row['config_module']][$row['config_name']] = $row['config_value'];

    // create the email configuration array
    $config = array(
        'useragent' => 'ExiteCMS v' . $this->config->item('version', 'exitecms') . '.' . $this->config->item('revision', 'exitecms'),
        'protocol' => 'mail',
        'smtp_host' => $this->config->item('smtp_host', 'exitecms'),
        'smtp_port' => 25,
        'smtp_timeout' => 15,
        'wordwrap' => FALSE,
        'wrapchars' => 76,
        'mailtype' => 'html',
        'charset' => 'utf-8',
        'validate' => TRUE,
        'priority' => 3,
        'crlf' => "\r\n",
        'newline' => "\r\n",
        'bcc_batch_mode' => FALSE,
        'bcc_batch_size' => 200

        // use SMTP instead of the internal mailer?
        if ( ! empty($config['smtp_host']) )
            $config['protocol'] = 'smtp';
            $config['smtp_user'] = $this->config->item('smtp_username', 'exitecms');
            $config['smtp_pass'] = $this->config->item('smtp_password', 'exitecms');

    log_message('debug', 'ExiteCMS configuration loaded');

    // load the email library

    // and initialize it
    $this->email->useragent = $config['useragent'];


My configuration table:
CREATE TABLE IF NOT EXISTS `cms_configuration` (
  `config_id` int(9) unsigned NOT NULL AUTO_INCREMENT,
  `config_module` varchar(50) NOT NULL DEFAULT '',
  `config_name` varchar(50) NOT NULL DEFAULT '',
  `config_value` tinytext NOT NULL,
  `config_timestamp` int(10) NOT NULL DEFAULT '0',
  PRIMARY KEY (`config_id`),
  KEY `config_module_config_name` (`config_module`,`config_name`)

El Forum

@WanWizard - I've been looking to make a solution just like this for some time. Is this being called from a custom library or when are you calling this function?

El Forum

I have a bit of an odd situation, and I don't use routes. All requests are routed to a single controller, that does the routing based on database information. I have this code in a library, which is called in the index method of this controller.

In a "normal" CI environment, you could call this from the constructor of a MY_Controller controller.