codeigniter tutorial

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 :-

$config['base_url'] = '';

Change it with the our project URL as shown below :-

$config['base_url'] = 'http://localhost/codeigniter/'

Removeing index.php from URLs

Open the project URL http://localhost/codeigniter/welcome in your browser, you will get the some error,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 :-

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

Replace it with the following line of code :-

$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:-

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

Now, visit the project URL http://localhost/codeigniter/welcome in your browser

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 :-

$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 hostnameusernamepassword, and database with your database details

Default Router in Codeigniter

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 :-

$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 in codeigniter

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.

$autoload['libraries'] = array('database', 'email', 'session');
$autoload['helper'] = array('url', 'file', 'form');
$autoload['model'] = array('kwikl3arn');