In this tutorial you will learn about the Codeigniter Configuration and its application with practical example.
Configuring base URL
The base URL defines the root of our application. Open the config.php file located in application/config/config.php directory.
Find the following line of code –
1 |
$config['base_url'] = ''; |
Change it with the our project URL as shown below –
1 |
$config['base_url'] = 'http://localhost/codeigniter/' |
Removing index.php from URLs
Open the project URL http://localhost/codeigniter/welcome in your browser, you will get the following error –
this is because codeigniter is a MVC framework and in codeigniter all requests go through index.php, this will work fine if you open above URL including index.php slug (http://localhost/codeigniter/index.php/welcome)
To fix the above problem, let’s open the config.php file located in application/config/config.php directory.
Find the following line of code –
1 |
$config['index_page'] = 'index.php'; |
Replace it with the following line of code –
1 |
$config['index_page'] = ''; |
Now, create a new file called .htaccess and place it inside your project root. Put the following lines of code in .htaccess file –
1 2 3 4 5 |
RewriteEngine on RewriteCond $1 !^(index\.php|resources|assets|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L,QSA] |
Now, visit the project URL http://localhost/codeigniter/welcome in your browser
Note:- You must have mod_rewrite enabled.
Database configuration
To setup database connection, open the database.php file located in application/config/database.php directory. Here you will find following lines of code, database configuration settings are stored in an array –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => '', 'password' => '', 'database' => '', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE ); |
Change the default values for hostname, username, password, and database with your database details
Configuring default route/controller
In CodeIgniter framework, default controller is the controller that automatically called when one isn’t specified. Open the routes.php file located in application/config/routes.php directory.
Find the following line of code –
1 |
$route['default_controller'] = "Welcome"; |
Currently, CodeIgniter have a default controller called “welcome.php” that is located in application/controllers directory. Change it with the controller name that you want to loaded as default one.
Autoload Configuration
Let’s open the autoload.php file located in application/config/autoload.php directory, this file is used to configure the libraries, helpers, languages and models that you want to be automatically loaded.
All of these configuration are stored as an array, you need to append libraries, helpers, languages and models name to the corresponding array that you want to be automatically loaded.
Example:-
1 2 3 |
$autoload['libraries'] = array('database', 'email', 'session'); $autoload['helper'] = array('url', 'file', 'form'); $autoload['model'] = array('user', 'blog'); |