How to read the config file data in the application of Codeigniter

In the earlier posts we learned about how to create baseurl, custom library, sending email in Codeigniter. Now we can learn about how to read the config file data in the complete application of Codeigniter. It is very tiny but useful when we need to use any variable in several times in the application.

How to read the config file data in the application of Codeigniter - Anil Labs - an Anil Kumar Panigrahi's technical blog

How to read the config file data in the application of Codeigniter – Anil Labs – an Anil Kumar Panigrahi’s technical blog

Below I have taken the example variables which I have taken in send emails using HTML templates in Codeigniter

Config.php code

$config['smtp_host'] = 'your domain SMTP host';
$config['smtp_port'] = 25;
$config['smtp_user'] = 'SMTP Username';
$config['smtp_pass'] = 'SMTP Password';

In your controller files or in view page we can directly access it by below code

$this->config->item('smtp_host');
$this->config->item('smtp_port');
$this->config->item('smtp_user');
$this->config->item('smtp_pass');

By this we can access these variables through out the application of Codeigniter. If you want to change any any value then change it in config file that will reflect entire application. It looks simple but we need implement it for huge application then debugging will be easy.

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online

Contact : [email protected]

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *