Codeigniter Config Class Library


Codeigniter Config Class Library– Configuration is an important part of any framework, codeigniter provides default configuration file where we can add the application configuration settings. $this->config->load(‘filename’); loads the configuration file. Sometimes we need to create custom configuration file, codeigniter provides an easy way to create custom config files. It is very simple to create custom configuration file in codeigniter. Here in this tutorial we are going to explain with various examples.


Codeigniter config class library | Load | Example

Let us understand how config class works in codeigniter with examples.

Load codeigniter config class library

How to load codeigniter config class library example:

 
$this->config->load('filename');

Where file_name is name of the configuration file that you want to load.

Working with config file :-

Let us understand how to use default configuration file and create own custom configuration file.

How to Create Custom Configuration File?

To create custom config file first go to config folder and create a file named config_custom.php in config folder.

Codeigniter Config Class Library
Codeigniter Config Class Library

You can load config files in following ways-

  • 1. Auto loading : Codeigniter has auto-load function which helps us to load library, helpers and models.
  • 2. Manual loading : We can load config files manually.

Functions:-

Two functions are available in config class library. Now we will explain one by one with example.

  • 1. Fetching config item.
  • 2. Setting a config item.

1. Fetching config items

EXAMPLE

You can fetch the config items simply as below-

How to fetch config items in codeigniter example:-


public function configure()
{
$this->config->load('custom_config');	
echo "Default Value <br>";
echo $this->config->item('tutorialsplane_main_url');
}

2. Setting a config item

EXAMPLE

Here is simple example of setting a config item.

Codeigniter set config item value example:-


public function configure()
{
 echo "After Updating Config Value<br>";
$this->config->set_item('tutorialsplane_main_url', 'tutorialsplane');
echo $this->config->item('tutorialsplane_main_url');
}

Class Reference

There are following types of class reference in config class.

1. Config

$config

This loads all config values.

2. Item

item($item[$index=''])

This rule is used to fetch an item from the configuration file.

3. Set item

set_item($item, $value)

Set Item function sets the value of item.

4. Load

load([$file = ''[, $use_sections = FALSE[, $fail_gracefully = FALSE]]])

Load function is used to load the configuration file.

5. Site Url

site_url()

Site Url function returns the url of your site along with the index.

6. Base Url

base_url()

Base Url function returns url of your site along with optional path like stylesheet or image.

7. System Url

system_url()

System Url function gives url of your codeigniter system/directory.


Advertisements

Add Comment

📖 Read More