Codeigniter for the absolute beginner.

Codeigniter for the absolute beginner.

I have seen in some forums and different websites, some doubts about how codeigniter is organized and how can it help you as a coder be more efficient. The first thing people should know is, codeigniter is a PHP framework, made with the software architecture known as MVC which stands for Model-View-Controller. This architecture seperates the logic from user interface allowing independant development, testing and better maintenance. Codeigniter allows you to have your code much organized, making you look better at what you already do. This framework is made to facilitate the way you do certain things in PHP, and allow you to reuse your code, using PHP Object Oriented programming paradigm. Codeigniter, on my opinion, seems to be very user friendly, so it’s to which I would recommend the use of it, for any upcoming web developer.

How to install Codeigniter

Some people start to doubt about the use of codeigniter when they hear or read the word ‘install’. In this case, you shouldn’t, the install process for codeigniter is farily simple, since all you need to do is, download the framework from their website, which can be found here:

Direct download is here: Latest Version

Next, after you have downloaded the files, just extract the content and place them into your root folder. That’s it! You can also rename your folder as you wish, so I suggest giving it a relevant name.

Config File

This config file can be found in root folder -> application -> config -> config.php. For this post and simple example, all we would need to do is change one line of code:

In my case, I have put my base_url as localhost/ci_basics. This tells codeigniter that the files for this project are in the folder named ci_basics. This should be all for now, since this example is very simple, and it’s just to let all of us know how simple codeigniter can be.

Routes File

The next thing that we should change is our routes file. This file can also be found in the same path as our config file, root folder -> application -> config -> routes.php. In this file, we would change our default_controller. Since we are going to name our first page ‘home’, and also, name our first controller home, then we can tell codeigniter where to look first.

That’s enough configuration for today. Now let’s create our home controller. Controllers are just PHP classes that are within the folder controller. Each class should have the same name as it’s file. Also, each class will extend from codeigniter’s main controller. Let’s see how.

File name home.php

In object oriented programming, the classes are made up of functions(methods in other languages). One can be a class construct. This construct will run everytime the class is called, or instantiated. In PHP, the way to declare a contruct has changed. For those of you who are new to this, then you might not notice the change, but for those of use who have been around for a while, will see that, PHP used to call class construct functions with the same name as the class. With PHP 5+, we now have another way of doing this. We would call the construct like this.

Update: As mentioned in a comment below from Tobz, in order to reach the parent constructor, you need to specify it in the code. (This slipped from me). If you do not add this line of code, you will get errors.

As mentioned above, this construct will execute everytime this class is instantiated. Constructs are good to have, to give initial values to variables. Let’s create a variable date, and give the value of today’s date.

Before we start displaying anything on the browser, I would like to recommend an organized structure for your files. In many cases, we will be using a CSS file, or a javascript file for all our pages. In this case, we can make a header file, a footer file, and files with main content.

First create a folder named includes. This folder should reside inside of: application -> views -> includes. Within this folder, create 2 files. One called header.php, and the other footer.php. The header file should look something like this:

Filename: header.php

Notice it has an open body tag, but no finishing. This tag should be finished in the footer file.

Filename: footer.php

For now, we can leave this as is. Let’s go back to our controller, and let us show the page to the user. Within the controller, we would need to call the ‘view’ function to display the content of the main content file to the browser. Let’s create an index function, and send the variable of today to the view, and display todays’ date to the user. Also, we will call another file that is within the includes folder called template. This template file will load the header, the content, and then the footer, and display it in the browser. We will create an array named $data. This will be an associative array, with data that will tell the template file, which content to load, and also, send in the variable of todays date. Once inside the view, this array is converted to variables. Let’s see how.

Inside the includes folder, we need to add a new file named template.php
Filename: template.php

Notice, how we load each view file without the extension of .php. Codeigniter will automatically look for a file with that name and give it the proper extension. (They should have .php extension though).

Now, let’s create our content file, which should go in our views folder. Within this file, we can access the variable $today, since it was sent through our $data array. This file can be as simple as the following example.

Filename: home_view.php

And there you have it (Codeigniter for the absolute beginner). This is technically the most basic codeigniter can get for anyone trying to step into this framework world. Hope you all enjoy, and remember, feedbacks are always welcome.

Related Posts

About the Author: Jorge Torres

MS. Computer Science Codeigniter Web Developer

21 Comments+ Add Comment

  • Thank you, Yotorres,
    This is really very most basic about CodeIgniter. I tried Zend, Joomla, CakePHP, and Drupal but I think that CodeIgniter is the best option for someone who know PHP and writes code.
    I am jobless now, so if you need a web programmer I am here.
    Mato Cica

  • Hi jotorres, is gr8 post.

    I am biginner to codeigniter.

    Do u have captcha code to implement in any form. can plz share here or to my mail.

    thanks in advance,

  • [...] I already convinced you, then give it a try and start learning it. Jorge Torres put a nice intro for CodeIgniter beginners that I bet you guys would like to read. After you read that, dig into Net [...]

  • Nice article.
    One thing, you’ve left out the call to
    at the start of your controller construct.
    People will run into problems without that.

  • Tobz! Thanks for that, I can’t believe I totally forgot. Honestly I don’t know why I forgot, but thanks, and I have fixed the post.

  • mister George!!!!
    Please recommend me a debugging tool for php.
    I havent done any research on it so i am going straight to the source.
    I am feeling lazy today :) ]

  • Hello there my friend, nice to see you around here! Debugging for PHP s a bit difficult as PHP does not have an internal debugger. There are exeternal debuggers though, at your disposal. There is the zend IDE which I think is the most commonly used.
    Another debuuger you might want to check out is DBG which can be found here:

    Lastly, you can try any of these 2 advanced debuggers for PHP:

    Hope that helps ney! Trust me, your gonna have fun.

  • graciassss por el tutorial, por fi pude instalar mi primera pagina en codeIgniter, gracias de peru !!!!

    • De nada amigo, cualquier duda o pregunta, la puedes hacer.

  • A great post Jo, for beginners I think it is a good starting point…

  •’s really great post for me coz i’m learner.

  • Its awesome for me because i am fresh with CodeIgniter. Thanks for this.

  • Nice one jo,i luv it coz am learning.
    But pls hw do we acces the view created from this tut on a browser ?

    • Hi Emeka, glad u liked the the post. In order to display the view on the browser, we go ahead and call the view function from the controller. Once we do this:

      Then, all we need to do in the browser is write the following URL:

      That’s all there is to it.

  • Nice one..Thank You….

  • Thank you so much for your tutorials. They are extremely helpful to me. I am a beginner. Do you have any suggestions on how to go about developing on my pc and then deploying the code to the web server?

    • Well, you can always code, then upload via FTP, unless you know what GIT is. If you have time, I recommend learning GIT, makes you have organized code, backups and better than FTP.

  • i m start today to learn codeigniter . i know mvc structure .plz give me good reference site to learn ci…..

  • Thanks a lot! This is the first tutorial on the Internet which explained everything step by step.
    I read few tutorials out there but none explained all stuff one by one.

    I had big problem with __construct while you made it so so so easy!

    Thank you again !!!

  • this is a best tutorial for first demo app

  • thanks for made it so easy for us.. keep it up

Leave a comment