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
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');
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;
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
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.
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.
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.
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.
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]
Now we have done all the configurations and settings to run the project. Lets move on to controllers in the next section.