How to Configure CodeIgniter

How to Configure CodeIgniter

Configuration of CodeIgniter

In our previous articles we have discussed about CodeIgniter and its installation process. Now we are going to introduce to you the configuration process of CodeIgniter. After setting up our site, the next thing that we should do is to configure our site. The application/config folder contains a group of files that set basic configuration of your site.

1. Configuring the Base URL

The base URL is the root of CodeIgniter and this URL can be configured in application/config/config.php file. You have to find the code written below in config.php file.

$config['base_url'] = '';

This code should be changed with your site URL :

$config['base_url'] = "";

2. Removing index.php from URLs

If we open our project using URL http://localhost/codeigniter/welcome we will get Error 404 i.e. Object Not Found. This error is occured due to MVC pattern of CodeIgniter and all request go through index.php. To fix this problem we have to find the code written below in config.php file

$config['index_page'] = 'index.php';

and replace it with the following code :

$config['index_page'] = '';

If you’re running Apache, you can do that by creating a redirect in your site’s .htaccess file. If you don’t already have one, just create a new file called .htaccess in your site’s web root.

Write the following code inside your .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Now visits your site using URL http://localhost/codeigniter/welcome it will run without index.php. For this you have mod_rewrite installed and enabled in your Apache server.

3. Database Configuration

The database of the site can be configured in application/config/database.php file. The configuration settings are stored in the array as shown below –

$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'database_name',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array()

In the above code you can change the default values for hostname, username, password and database according to your database details.

You can simply switch to different environment by changing the value of a variable as shown below –

$active_group = ‘default’; //This will set the default environment
$active_group = ‘test’; //This will set the test environment

4. Autoload Configuration

Autoload Configuration file specifies, which systems should be loaded automatically. In order to keep the framework as light-weight as possible, only the absolute minimal resources are loaded by default. Following are the things you can load automatically –

Libraries – It is a list of libraries, which should be auto loaded. Provide a list of libraries in an array as shown below to be autoloaded by CodeIgniter. In this example, we are auto loading database, email and session libraries.

$autoload['libraries'] = array('database', 'email', 'session');

Drivers – These classes are located in system/libraries/ or in your application/libraries/ directory, but are also placed inside their own subdirectory and they extend the CI_Driver_Library class. They offer multiple interchangeable driver options. Following is an example to autoload cache drivers.

$autoload['drivers'] = array('cache');

Helper files – It is a list of helper files, to be autoloaded. Provide a list of libraries in the array, as shown below, to be autoloaded by CodeIgniter. In the given example, we are autoloading URL and file helpers.

$autoload['helper'] = array('url', 'file');

Custom config files – These files are intended for use, only if you have created custom config files. Otherwise, leave it blank. Following is an example of how to autoload more than one config files.

$autoload['config'] = array('config1', 'config2');

Language files – It is a list of language files, which should be auto loaded. Look at the example given below. Provide a list of languages in an array as shown below to be auto loaded by CodeIgniter. Keep in mind that do not include the “_lang” part of your file. For example, “codeigniter_lang.php” would be referenced as array(‘codeigniter’);

Models – It is a list of models file, which should be autoloaded. Provide a list of models in an array as shown below to be autoloaded by CodeIgniter. Following is the example of how to auto load more than one models files.

$autoload['model'] = array('first_model', 'second_model');

Please let me know if you have any query related to CodeIgniter we will provide you best solution for the same.You can write in the comment box below.

Back to Top