Skip to content

Codeigniter – Save config data in database

Last updated on May 6, 2018

In this post, I would like to show you saving config data in the database in your CodeIgniter application. And I will show you, How you can get these data when you need to use in your project.

Codeigniter – Save config data in database

To achieve this you are going to use CodeIgniter hooks. To initialize the hooks class one must have to enable it as shown below –

Open application/config/config.php file and set enable_hooks config value to true.

   $config['enable_hooks'] = TRUE;

Steps to Save Codeigniter config data in database

There are several hook points available in CodeIgniter. As for current requirement you need use “post_controller_constructor” hook point. let’s open application/config/hooks.php file and add following code. as you enabled hooks with above config option, hooks.php file will be auto-loaded by CI.

$hook['post_controller_constructor'] = function() {

	$CI =& get_instance();

	$appConfigOptions = $CI->AppConfigModel->get_configurations();

	if($appConfigOptions) {
		
		foreach($appConfigOptions as $appConfigOption)
		{

			$CI->config->set_item($appConfigOption->key,$appConfigOption->value);

		}

	}
};

Code Explained

post_controller_constructor – Called immediately after your controller is instantiated, but prior to any method calls happening.

$CI = &get_instance(); – This will return “reference of the CI superobject”.

$CI->AppConfigModel->get_configurations() – From the CI superobject, accessing auto-loaded modal “AppConfigModel” and reading get_configurations() method. This method will read database and return all the available config data.

After that, iterating over the data and by using CI superobject accessing set_item() method via config object and setting key and value of the config data.

Here is the auto loaded Config options – For application/config/autoload.php file

$autoload['libraries'] = array('database');
$autoload['model'] = array('AppConfigModel');

Here is the table schema –


--
-- Table structure for table `app_config_options`
--

CREATE TABLE IF NOT EXISTS `app_config_options` (
  `key` varchar(255) NOT NULL,
  `value` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `app_config_options`
--

INSERT INTO `app_config_options` (`key`, `value`) VALUES
('site_description', 'Web developments by arjunphp.com'),
('site_name', 'arjunphp.com');
ALTER TABLE `app_config_options`
  ADD PRIMARY KEY (`key`);

Here is AppConfigModel Model – application/models/AppConfigModel.php

table = 'app_config_options';
        }

        public function get_configurations()
		{
            $query = $this->db->get($this->table);
            return $query->result();
		}
}

That is it, now you can access config data from anywhere in the system by using this script config->item('sitename'); ?>.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments