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?
Config – This 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
July 28, 2008 at 9:09 am
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?
August 22, 2008 at 2:32 pm
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.
August 22, 2008 at 8:10 pm
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”.
September 19, 2008 at 3:50 pm
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.
June 17, 2011 at 11:44 am
http://localhost/codeigniter_1.6.3/index.php
try like this
abdul
April 8, 2009 at 3:09 pm
For a newbie like me, this is very helpful. some tutorials assume that you know something off hand but this got me going. thanks
August 6, 2009 at 9:26 pm
[...] 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 [...]
August 16, 2009 at 4:33 am
[...] CodeIgniter 3 Part Hello World Tutorial [...]
August 16, 2009 at 5:04 am
[...] view plaincopy to clipboardprint? [...]
September 26, 2009 at 7:02 am
Thanx for this nice and simplest tutorial.
October 8, 2009 at 7:28 pm
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?
October 8, 2009 at 9:58 pm
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).
May 12, 2010 at 7:06 am
[...] view source print? [...]
June 3, 2010 at 10:42 am
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!
July 14, 2010 at 3:12 pm
This tutorial is shit…
August 30, 2010 at 10:14 am
Nice for novice
March 26, 2011 at 9:30 am
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…
May 3, 2011 at 12:16 pm
[...] CodeIgniter 3 Part Hello World Tutorial [...]
June 21, 2011 at 5:48 am
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?
June 27, 2011 at 12:44 pm
hanx for this nice and simplest tutorial.
January 31, 2012 at 6:11 am
[...] http://opinionatedcoder.wordpress.com/2008/05/17/code-igniter-tutorial-part-1-hello-world/ [...]
March 2, 2012 at 12:29 pm
The very model effortlessly hamburgers that affluxion onto piecemeal damask hailstorm spears. http://puvfol.com
May 22, 2012 at 1:02 pm
[...] [...]
June 4, 2012 at 7:21 am
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.
July 10, 2012 at 9:22 am
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.
July 26, 2012 at 1:44 pm
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?
August 24, 2012 at 11:37 am
I want to correction here
class Hello extends CI_Controller {
instead of
class Hello extends Controller{
September 30, 2012 at 12:09 am
Apa perbedaan nya pake TinyMCE sama XINHa di code igniter?
Trus kenapa di CI 1.7 ngga ada Library nya ya?
October 5, 2012 at 12:28 am
Awesome Tutorial!
November 19, 2012 at 6:57 am
Fatal error: Class ‘Controller’ not found in C:\xampp\htdocs\CI\application\controllers\hello.php on line 4
please slove this problem
February 21, 2013 at 2:13 pm
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):
February 21, 2013 at 2:17 pm
February 21, 2013 at 2:18 pm
class Hello extends CI_Controller{
function index(){
echo “Hello World!”;
}
}