Special Offer - Get $90 Discount and Host Your Dream Web Site @ Dreamhost for 1 Year Click Here. Promo Code SLJ345
Back To Top
WordPress Web Application Development
Develop Powerfull Web Applications Using Cutting Edge WordPress Development Techniques

Installing and Configuring Codeigniter using the Config Files

on 2011/05/24 5:15 AM stored in: Uncategorized and tagged:

Downloading and Installing CodeIgniter

In this tutorial i am going to explain how to install and configure codeigniter. Lets start by downloading CodeIgniter.Go to http://codeigniter.com/downloads/ and download the latest version of Codeigniter. Current latest version is 2.0 and it has some changes in the way it is used compared to previous versions.

 

Now extract the downloaded folder to your web sever root and rename it to your project name. Lets assume your project name is InnovativePhp. You can access your project using http://localhost/InnovativePhp . You can see the default codeigniter welcome page.

 

Make sure you give the necessary permission to your project folder if you are working on linux environment. Lets start configuring the project.

CodeIgniter Configuration Settings For Your Project

Now that we have successfully installed Codeigniter lets start configuring the project. All the configuration files related your project is stored in the config folder, inside your application folder. In this tutorial i am going to explain the functionality of main configuration files.

Autoload Configuration

Autoload configuration allows you to load files when ever you required without including manually. Open the autoload.php file inside application/config folder. All the autoload configurations are separated into sections.

 

Auto-load Library Section

This section allows you to load codeigniter system libraries automatically. If you have functions that uses one or more libraries regularly you can load them automatically by changing the code to the following.

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

This code will autoload the database and session libraries inside system/libraries folder.

 

Auto-load Helper section

This section allows you to load system helpers automatically.This following code will load the url helper inside system/helpers folder.

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

You can also configure other options like language,models and custom config files by using the above method and changing the auload file.

 

Important - Make sure you use the autoload configuration for things you use more regularly since it takes lot of system resources if you load all the files automatically when they are not needed. If you choose not to autoload files you have to load them inside your controller functions using the following code.

$this->load->library('session');
$this->load->helper('url');
$thia->load->model('sampleModel');

General Configuration

General project configurations are done on the config.php file inside system/config folder. Open the file and you will see all the configuration details about your project. In this section i am going to explain the most important configuration settings.

 

base_url for your project is the most important setting you have to configure. When ever you call a file ( form action or hyperlink) baseurl should be provided. Codeigniter identifies the path to your files using the baseurl. Since our project name is InnovativePhp, you have to change the baseurl to http://localhost/InnovativePhp/ on the config file.

$config['base_url']     = 'localhost/InnovativePhp/';

There are other optional configurations you can set according to your preferences. Session variables in the config file is one of the most important optional parameters you have to configure.

 

You can set the session expiration time by using the sess_expiration setting in the config file. Its default value is 7200 seconds. That means every 2 hours system will clear the session data and hence your logged in users will be logged out after 2 hours. You can change the value of this to whatever you need. If don’t want to clear the session after certain period just set the value to 0.

$config['sess_expiration']              = 7200;

Database Configuration

Database configurations are done on the database.php file inside system/config folder. Open the file and you will see all the configuration details about your project. In this section i am going to explain the most important configuration settings.

 

To use a database for your project you need to have a database user and a database. So you can set the database user settings using the following code inside database.php file. Hostname will be your server url. If you are working on local machine it will be localhost and if you are working on the server it will be the database server url provided by your hosting company. Then you need to specify the database users username, password and databse name in the given fields.

 

$db['default']['hostname'] = ‘localhost’;
$db['default']['username'] = ”;
$db['default']['password'] = ”;
$db['default']['database'] = ”;

 

You can choose from wide range of database types like mysql,posgretesql… and specify your database type in the dbdriver field.
$db['default']['dbdriver'] = ‘mysql’;

Setting The Url Structure

Setting the url structure is one of the most important things to do before you start coding the project since it affects the coding technique and other factors like SEO,Security and User friendliness.

 

SEO – Url plays a key role in search engine optimization. Your url should have your keywords in order to rank high for your targeted keywords. For example consider the following url which contains the main function and parameters separated by ‘&’ s.

 

www.example.com?page=php_quiz&name=Adam

 

This url is not profitable when you look at it in the seo perspective. Here your keyword will be php quiz. But it is in the parameter list separated by a ? mark and hence it will not considered for ranking by most of the search engines. Instead search engines travel through sub urls. For example in the following url your keyword is mentioned as a sub component of the url not as a parameter. So it will be considered by search engines in their rankings.

 

www.example.com/php_quiz/Adam


Security – You have to consider about security of the project. If you use the default way anyone will be able to access your files using the folder name in the url. In order to block these access to unwanted url you have to use htaccess files and redirect requests according to the url.

 

Now considering all the factors mentioned above lets move on to creating our url structure. Codeigniter does not come with a default .htaccess file. Hence we have to make our own .htaccess file to specify the url structure. Consider the default url structure of codeigniter mentioned below.

 

http://www.example.com/index.php/php_quiz/register

 

In this url there is a part called index.php after the server name. All the requests to the website will be handled through this index.php file. Then there is 2 components called php_quiz and register. Here the php_quiz is the name of your controller class name and register will be the controller class function name. I am going to talk about controllers in the next section so be patient.


Meanwhile ill explain about the quality of this url. Here the second component will be index.php. So you cannot improve the search engine rankings if you include this part because search engines reduces the importance in the url part when the depth of the url increases. Basically what it means is that index.php will have a higher importance than php_quiz and register will be considered least important. Since your keyword is not included in index.php you will not get the optimum results. we can solve this problem by removing index.php from url and making php_quiz the most important part. This can be achieved by using the htaccess file.

 

I am going to explain about .htaccess files and redirects in another tutorial. Since then ill give a sample .htaccess file to include in your projects to achieve required results. The code for the htaccess file is given below.


 

RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|uploads|swf|js|language|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

 

After using this .htaccess file all the requests will be redirected and handled through index.php automatically and the users will not be able to directly access the images,css,uploads,swf,js,language directories. So your new url should be in the following format.

 

http://www.example.com/[controller class name]/[controller class function]

Ex: http://www.example.com/php_quiz/register

 

Now we have done all the configurations and settings to run the project. Lets move on to controllers in the next section.

Leave a Reply

Follow Us On Facebook