Hi guys! Welcome back to CodeIgniter tutorials. Today I thought to explain the basic things on starting a project using CodeIgniter. What are the basic configurations? Here I will exactly mention the essential configurations needed to run any project using this framework.

  • Configure base_url()

This is the very first thing to do after extracting the CI folder into your project folder. This is the path for your project folder. You need to navigate to the config folder in the project folder. Open this config.php file and find $config['base_url'] line.

File path: application/config/config.php

Since we run in on localhost, path will be like this.
$config['base_url'] = 'http://localhost/MainProjectFolder/CurrentProjectFolder';

Ex: http://localhost/Projects/ci_project

  • Configure database.php file

DB connection can be built at one place. This is the file that connects our project to database. You need to navigate to the config folder in the project folder. Open database.php file and go the bottom line of codes.

File path: application/config/database.php

Create a database and give its name, username , name of your server and password. Usually default password is -> " ".

'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'database_name',

  • Configure autoload.php file

This is also a very important configuration. What is this autoload file? It provides the CI framework support for any CI project. Autoload file is used to include CI libraries, helpers, packages, drivers and many other things. Usually for any project, there are some essential libraries and helpers to be included. When we include them here, those mentioned items are loaded automatically for each and every page that we run through the project. No need of loading them in every page. CI does that job for us using this autoload file.

File path: application/config/autoload.php

Libraries needed usually : database, session, form_validation

database is  essential for any project since every PHP project is connected with a database. If you are dealing with sessions, load the session library. When you need to take inputs through a HTML form and validate them, use form_validation library. They are optional depending on your requirements.

Helpers needed usually : form, url

Include them according to the below format.

$autoload['libraries'] = array('database','session','form_validation');
$autoload['helper'] = array('form','url');

  • Configure routes.php file

Since CI is based on MVC architecture, when you need to load a page it should be done through a controller.

Usual format: localhost/project/index.php/controller/function/parameters

When you need to load the project, without typing a controller in the URL, you have to configure the routes.php file. Go to the config folder and find this file.

File path: application/config/routes.php

Go to the line containing "default_controller" word. Then assign  here your main controller that loads your project. By default they have mentioned it as "Welcome". It is the CI default controller. 
So you do not need to type controller "Welcome" in the URL bar to run the default CI project. Here, you are doing the same thing.

After assigning;

$route['default_controller'] = 'Your_Default_Controller_Name';

  • Load model at one place for all functions of a controller

In CI, when a web page needs some data from the database, you can not take them directly to the view file. It should be come through a model function. I think you know it! So, when you are creating functions in the controller, when you need to fetch data, what will you do? Model should be loaded and then function should called. Every time you need the help of model, you need to load it. Now I'm going to make things easier. Include the following function as the very first function in your controller. Then you are free from model..! No need of loading it in the functions within the particular controller.

    public function __construct()
    {
      parent::__construct();
      $this->load->model('Model_Name');
    }

  • Remove Index.php (Optional)

When you need to load a particular function through a controller, there's something in the URL always. That is this index.php part. Without including it, you can not access to any function in a controller. Since we are lazy to type, we can get rid of this! You have to do something simple.

You need to navigate to the config folder in the project folder. Open this config.php file and find $config['index_page'] line.

File path: application/config/config.php

Now you can see they have given a value to this as "index.php". Remove this part from the file and save it. If you are with me, this is the way to do it.

$config['index_page'] = '';

Go to project folder and create a file called ".htaccess" and include the below code in it.  Remember, this should be created in the project folder, without going into any sub folder. OK? Then you can access anything in controller without index.php!

RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]

Now you are ready to start any project! After these configuration, go for a project. It will make your life easier. I have experience of working with CI without knowing these basics. Then I found them and still I'm following these steps whenever I start a CI project.

Hope you get some idea..Try these steps..
Good Bye!

5 Comments

  1. This information you provided in the blog that is really unique I love it!! Thanks for sharing such a great blog. Keep posting..
    PHP Training
    PHP Course
    PHP Institute

    ReplyDelete
    Replies
    1. Thank you Shailendra! Keep in touch..I'll post more..

      Delete
  2. You are awesome because the post you've shared here is interesting and informative also. Thanks for posting it. Keep it up. php course in delhi

    ReplyDelete
  3. The post you've shared here is great because it contains some great knowledge which is extremely useful on my behalf. Thanks for posting it. Keep posting. PHP Training in delhi

    ReplyDelete