Codeigniter, PHP

Codeigniter: Getting Started

Codeigniter | Controller | Framework | PHP | View



This entry is part 1 of 8 in the series Learn Codeigniter

What is Codeigniter?

Codeigniter is a MVC based framework for PHP. Which enables programmer to build web application in modern way. if you dont know what MVC is then please refer to the previous tutorial : Introduction to MVC Architecture. Codeigniter is an open source platform to develop web application so if required we can add more libraries and functionality.

First download the codeigniter. Which is open source. Extract codeigniter in your webserver’s root directory in my case its c:\wamp\www\. Lets name it simply ci but you can give it any name you want. So our codeigniter files are in c:\wamp\www\ci directory. You can see there’s three directories:

  1. application
  2. system
  3. user_guide

In your case it may be different, depending upon which version of codeigniter you are using. I am using codeigniter 2.1.2. User_guide directory contains guide for programmers which i dont need so generally i delete that one. The System directory contains core framework of Codeigniter so most probably we dont change that one. Last directory which is application, where we put and configure our site.

Application directory contains some more directories but the important directories for us right now are:

  • config (contains configuration of website)
  • controllers (contains Controller classes)
  • views (contains various views)
  • models (contains models of the websites)
Codeigniter Directory Structure

Configuration for Codeigniter website

In application/config/config.php change the line which has setting for base_url. Just find the base_url and locate line which says $config['base_url'] = ”; and change that according to your codeigniter directory mine has following:

this will set base url to your website root directory which we will user later as we learn more.

Next step is to set route.php file in config folder. set default_controller to home. config files follows key value pair to store configuration like $config['key'] = ‘value’.

This will set our default_controller to home controller which we haven’t created yet. so if we run website by loading http://localhost:8888/ci/ in browser it shows 404 page not found error generated by codeigniter. but dont worry about that we will create our first controller in short time :-) .

Next step is to configure database.php file located in config directory. set database details in database.php.

configure above marked lines according to your database details. and that’s it for configuration. This few steps we have to follow everytime we want to create new website using codeigniter :-) .

Creating First Controller (Home Controller)

Create new file named home.php inside application/controllers directory. and put following code in that file.

In above code we created a class called Home (remember the H is capitalized, that is important) and extend it with CI_Controller which is the base class for every controller. Create a function called index and echo a line.

Navigate to  http://localhost:8888/ci/ now you can see that message in browser. Now you are wondering how this works? Remember we configure our route.php by specifying default controller to home. That will load home.php file from controllers directory and because we didn’t specify which method to load it will load index method by default.

Lets add another function to the home controller called about.

add highlighted code to home controller now if you load http://localhost:8888/ci/ you will see same page with message Welcome to Home Page and not the About Us Message because index function will be loaded by default and we haven’t specified the method. lets now specify about method in url.


Remember we have to put index.php although we can remove by some settings but for now lets leave it there. next element in URL specifies which controller to load. In above URL home controller will be loaded. Next element specifies which method to load. In our case it will be about. now if we navigate to that URL about us message will appear. If we wont specify method name default method is index.php.

So pattern for URL is like : http://PATH_TO_YOUR_WEBSITE/index.php/controller/method/

But we are not going to echo html from controller, that’s a bad habit. we will create separate file for presentation (view) cause we are using MVC architecture we have views directory which contains different views for our pages. So lets create one view for our home page.

Create First View (homepage view)

Create new file in application/views/ directory and name it whatever you want, I name it homeview.php (we can also create directory and then create view inside that directory). put the following code in homeview.php.

and now modify the home controller’s index function to load newly created view as below:

instead of echo message we loaded a view called homeview. codeigniter uses this syntax for loading view. So homeview.php will be loaded from views directory and if its not present then it shows error message (Unable to load the requested file: homeview.php) but we have that view so it shows our home page.

Now navigate to http://localhost:8888/ci/

which loads default controller (home) and default method (index) so homeview.php will be loaded and you will get result similar to this:

Codeigniter First Page

You can also add another view named about.php in views directory and load it in home controller’s about method. I am not doing this here consider it as your homework :-) .

We have loaded view from controller but we haven’t pass any data between controller and view yet. That’s our next task and we will cover that in our next tutorial.

I hope you like this tutorial :-) . Thank you.

Series NavigationCodeigniter Model: How to fetch Data From Database >>

Bookmark Link using: bookmark at folkd