Codeigniter create custom config

Codeigniter create custom config

Create config file in ‘application\config’ folder, give any name (for example I have given ‘cust_settings.php’ [application\config\cust_settings.php] )

<?php  
if (!defined('BASEPATH')) exit('No direct script access allowed');


$config['site_name'] = 'My website';
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp1.server.com';
$config['smtp_user'] = 'username';
$config['smtp_pass'] = 'password';
$config['smtp_port'] = '25';

You can use ‘config->load()’ function to load the configuration and ‘$this->config->item()’ to retrieve the configuration values

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

	public function index()
	{
		

		// Loads a config file named cust_settings.php and assigns it to an index named "cust_settings"
		$this->config->load('cust_settings', TRUE);

		// Retrieve a config item named site_name contained within the cust_settings array
		$site_name = $this->config->item('site_name', 'cust_settings');

		echo $site_name."<br><br>";

		// An alternate way to specify the same item:
		$cust_settings = $this->config->item('cust_settings');
		
		//$site_name = $cust_settings['site_name'];

		echo "<pre>";
		print_r($cust_settings);
		echo "</pre>";

                $this->load->view('welcome_message');


	}
}

Output

For using this we can load multiple configuration files

 

ref: codeigniter.com

 

Author: bm on January 24, 2018
Category: codeigniter

Your comment:

Your Name

Comment:




Last articles