Code Igniter Tutorial – Part 1 – Hello world

What this tutorial series will cover

In this series, I aim to give you an overview of how Code Igniter works, how it can give you nice clean URLs, how to use the MVC in your applications and how to interact with databases from within Code Igniter.

We will be building a simple application for managing todo lists, where the items are arranged in lists, and can be assigned to categories. I may even further extend this tutorial to add more functionality, and show how to create your own libraries which you can reuse in your own applications.

Installing Code Igniter

This is the first in my tutorial series, and we will discuss the very basics of setting up the Code Igniter environment, checking that all works, and creating a basic Hello World page.

First of all, you need to have a local web development environment. I’m using the XAMPP for linux package, as it provides an all in one solution, including apache, mySQL, phpmyadmin and a range of other stuff you don’t need to worry about. If you don’t have XAMPP, download it now (available for Windows, Linux, Mac and Solaris). Once you’ve installed that, you’ll need to download the latest version of the Code Igniter framework, which is is version 1.6.2 at the time of writting.

Once you’ve got XAMPP up and running (if you have any problems, consult the XAMPP documentation, trouble shooting XAMPP is beyond this tutorial), unzip the Code Igniter file you just downloaded into the web root (htdocs in the directory you created XAMPP).

Now if you go to http://localhost/CodeIgniter_1.6.2/, or whatever version you installed, you should see the default Code Igniter welcome page. If you don’t, you most probably didn’t start the XAMPP server correctly.

Understanding the file structure, and moving things around

Now if you look inside the Code Igniter folder, you should see index.php, license.txt, system and user_guidhttp://localhost/index.php/helloe. Now this default file structure works fine, but i prefer some seperation. This is entirely optional, but for the rest of the tutorial, I will assume you have your structure as follows.

In the system folder, you’ll find an application folder, this is the blood and guts of our applications, and is where almost all the code you write will go. Everything else in the system folder is the framework itself, so you don’t need to worry about it. Move the contents of the application folder up a level, so its in the same directory as index.php. It is also recommended that you move the framework itself out of the web root as you won’t need to access it directly. I recommend moving it to be above htdocs, and rename it to Code_Igniter_1.6.2, this way, you can have several versions of the framework installed, and switch between them easily. Finally, move everything index.php and todo into the web root. At this point you may delete the CodeIgniter_1.6.2 folder in the web root (which has got the user_guide and license.txt, both of which are online). You should now have just index.php and the contents of the application directory in the web root, with the framework one level above.

Now if you go to http://localhost/, you should get an error. This is because we’ve rearranged the file structure, and not told it where to find everything. To fix this, we need to edit index.php, so open it up in your favourite text editor. On line 26 (or there abouts) you should see it declare the location of the system folder, $system_folder = “system”;

Change this to point to your code igniter installation, which is now ‘../Code_Igniter_i.6.2′, and change the application path to be a period (current directory). If you now refresh your browser, you should once again see the welcome page.

So whats in each directory?

ConfigThis is where you do all the boring site configuration, such as setting up a database connection, site wide configuration and URL handling amongst other things.

Controllers – This is where you write your program logic, with a file for each class, made up of functions, more on this later.

Errors – Just defines what the various error pages are like, such as 404 errors etc.

Helpers – These are site wide collections of functions which you can write yourself, you won’t need to do much here.

Hooks – Not important at this stage, might talk about them at a later stage.

Language – Not important unless you plan on dealing in more then one language.

Libraries – Groups of related functions, which you can use i your controllers.

Models – Used to represent objects, do all the interaction with the database.

Views – This is where all the lovely HTML goes that the end user sees.

Hello world

So lets finally get down to it then.

Create a new controller called hello.php in the controller directory, snd enter the following code into it.



<?php
  class Hello extends Controller{
    function index(){
      echo "Hello World!";
    }
  }
?>

 

Seeing is believing

So now, go have a look at what you just done... go on, I'll wait.

Now matter how many time you refresh, you still get the welcome message, this is because we need to explicitly tell Code Igniter where to find the page we're looking for, or explicitly tell it in a configuration file. So lets tell it where to find our new Hello controller. Point your browser to http://localhost/index.php/hello, and you should see a message saying Hello World!

In the next installment, I'll show you how to set the default page to be your Hello world message instead of the welcome screen, and how to get rid of the need for index.php/... in the URL, and how to use views for output.

I hope you enjoyed this little tutorial, and found it useful, if you have any comments or came across any problems, let me know in the comments.

If you liked this, and would like to be notified of future posts and tutorials, please subscribe to my RSS feed.

Part 2 is now online

About these ads

33 Responses to “Code Igniter Tutorial – Part 1 – Hello world”

  1. Mudassir Says:

    I m getting

    Warning: require(./config/constants.php) [function.require]: failed to open stream: No such file or directory in D:\xampp\Code_Igniter_1.6.2\codeigniter\CodeIgniter.php on line 52

    Fatal error: require() [function.require]: Failed opening required ‘./config/constants.php’ (include_path=’.;D:\xampp\php\pear\’) in D:\xampp\Code_Igniter_1.6.2\codeigniter\CodeIgniter.php on line 52

    errors! how to solve it? plz tell me! Im not getting where to change application path and what to change?

  2. Mon Says:

    I personally found moving the “application” folder out of the system folder is such a big hassle, especially this is super simple tutorial and it just made things confusing. Although I see moving them to above htdocs, but not if you have multiple domains hosting, that could really screw things up. I would just keep the application folder default inside the system folder for the purpose of this tutorial.

    @Mudassir

    That’s the problem I had, too and like I said above, just put the application folder back inside the system folder, change the config back to “system” then you should be fine.

  3. Mon Says:

    I also forgot to thank you for writing this tutorial even though I found the moving part a bit confusing.

    @Mudassir

    If you’re sticking with the author’s file structure, to fix your problem. You will also have to change “application” to “../application”.

  4. Marjorie Says:

    Hey Damian,
    Why are we having problems with moving the “application” folder out of the system…
    I am working with mamp, local
    my path is:
    htdocs/CodeIgnater_1.6.3/index.php

    and I am getting errors..
    hope you can help, I want to move to the next tuto.

  5. Kaiser Says:

    For a newbie like me, this is very helpful. some tutorials assume that you know something off hand but this got me going. thanks

  6. First Days with CodeIgniter and MVC | JoshSweeney Says:

    [...] including how to get the index.php segment out of the middle of my URL. A little searching provided this tutorial which was exactly what I was looking for. The tutorial walked through the best way to rewrite the [...]

  7. Murado » Codeigniter (2) Says:

    [...] CodeIgniter 3 Part Hello World Tutorial [...]

  8. Murado » Codeigniter (3) Says:

    [...] view plaincopy to clipboardprint? [...]

  9. Khaled Jamal Ahmed Says:

    Thanx for this nice and simplest tutorial.

  10. Marcus Says:

    why there are no answer to people’s problems, i have tried this just as explained bu just like the other people i get the exact same error:
    Warning: require(./config/constants.php) [function.require]: failed to open stream: No such file or directory in D:\xampp\Code_Igniter_1.6.2\codeigniter\CodeIgniter.php on line 52

    Fatal error: require() [function.require]: Failed opening required ‘./config/constants.php’ (include_path=’.;D:\xampp\php\pear\’) in D:\xampp\Code_Igniter_1.6.2\codeigniter\CodeIgniter.php on line 52

    I think maybe showing the file Hierarchy like a screen shot or something would help alot since it is realy confusing, i have been playing for nearly 2hours and not happy. I have tried puthing the directory within my public_html and above it and i get the same error. can some one please help?

  11. Marcus Says:

    OK, since I spent like 2 hours on this problem I though. I’s share it with you .
    OK, the problem first was that when it says:
    “ Change the application path to be a period (current directory) “
    If you don’t know that there is another setting:
    $application_folder = ” “;
    Then you do not know what to do, it makes you think though :-)
    Now, once after say half an hour I find out that there is such setting.
    Then I thought, I should put “ ../ application ” since i read that some where in the manual.
    But no…. then I thought ok let’s just put what it says in the tutorial which was (current directory).
    $application_folder = “(current directory) “;
    I know stupid!

    Ok anyway the answer is really that you put just ” ../ “. But that does not worked for me. So if your hosting is like mine and your root is the public_html folder then this is how you want your settings to be:
    index.php:
    $system_folder = “../CodeIgniter1_7_2″;
    $application_folder = “../public_html”; or
    and don’t forget to change the config.php:
    $config['base_url'] = “http://yoursitename.com”;
    That’s how it worked for me. I am happy again! the thing is i knew this is the right way of setting your files. So it maybe a bit complicated but it is worth it, as you can do much more this way (see code completion tips and you know what i am talking about).

  12. Jiten Says:

    Hi Damian,
    It was such a beautiful and quick starter for me.
    I liked specially abstracting the system and application..
    and points about having multiple versions of codeIgniter.

    Thanks a lot !
    Great going!

  13. Kirill Says:

    This tutorial is shit…

  14. chlem Says:

    this will be the first time that i will be using code igniter as my framework and i’m a bit curious how is this framework going… i hope that i can do better with this framework…

  15. Mari Belajar Bersama – CodeIgniter Where do I start? Says:

    [...] CodeIgniter 3 Part Hello World Tutorial [...]

  16. Anonymous Says:

    Hi! First I am getting the Timezone Error. Once I set the Timezone, I am getting a blank page. Anyone any idea why this is happening?

  17. umer singhera Says:

    hanx for this nice and simplest tutorial.

  18. angel1971 Says:

    The very model effortlessly hamburgers that affluxion onto piecemeal damask hailstorm spears. http://puvfol.com

  19. here Says:

    As soon as I initially commented I clicked on the Notify me any time new comments are added checkbox and now each time a remark is added I receive four emails with the exact same comment.

  20. Chandan Upadhya Says:

    Thanks, this is so helpful…
    In CodeIgniter 2 your controllers inherit from super class CI_Controller, rather than the super class Controller used in CodeIgniter 1.

    The same applies to models in CI2 which extend the class CI_Model rather then Model.

  21. Anonymous Says:

    I am do all the things u have to tell above but No any character seen in my screen ……….plz tell me what happen …urjent?

  22. Jay Bharat Says:

    I want to correction here
    class Hello extends CI_Controller {
    instead of
    class Hello extends Controller{

  23. Galaxy Techno Says:

    Apa perbedaan nya pake TinyMCE sama XINHa di code igniter?
    Trus kenapa di CI 1.7 ngga ada Library nya ya?

  24. prasad Says:

    Fatal error: Class ‘Controller’ not found in C:\xampp\htdocs\CI\application\controllers\hello.php on line 4
    please slove this problem

  25. dave Says:

    OK I got the same error too ‘ Class ‘Controller’ not found in C:\xampp\htdocs\CI\application\controllers\hello.php on line 4′

    I had to change hello.php to the following (since I was using Codeigniter 2):


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: