Step 1 – About CodeIgniter
CodeIgniter is PHP framework with a rich set of libraries, helpers and plugins for PHP Developers who are in the real world of clients who want things “now” and shared hosting accounts. CI helps you build full-featured web applications with a small footprint, without the use of the Terminal or Command Line Interface. CI is compatible with PHP4 and PHP5, which is a great thing to have in a framework if you plan to use your scripts for different clients. This is a very simple framework to use, and as Rasmus Lerdorf stated he liked it “because it is faster, lighter and the least like a framework.”
CodeIgniter has many features that makes it stand out from the crowd: a powerful autoloading feature enables you to load components for use throughout the whole of your application; a routes file enables you to re-write the way their URL’s work; and it boasts a powerful database class that lets you use multiple databases such as MSSQL, MySQL (and MySQLi), PostgreSQL, SQLite, and ODBC – enabling you to switch databases without change to your codebase.
CodeIgniter has a strong following, and if you need something that isn’t in the core, you are highly likely to find it on the forums or in the wiki. With many solutions for User Authentication, CRUD and Template libraries, as a developer you really are spoiled for choice.
A quick overview of MVC
CodeIgniter uses the Model-View-Controller design pattern; this separates business logic (controllers) from the front end (views) and the database abstraction (models). MVC keeps your code structured and enables you to write more robust applications. For a more detailed overview, please consult the user guide on controllers, views, and models.
CodeIgniter URLs
Before you start developing an application in CodeIgniter, you need to know how that the URLs are constructed using URIs, which is the string following index.php in the URL. There are 3 segments to the URLs, the first is the controller, if you only supply the first segment, CodeIgniter will default to that controllers index function. The second parameter is the function inside the controller. The third represents any data you want to pass to your function. So when looking at a CodeIgniter URL, you will see something like this.
http://www.yourwebsite.ext/ci_dir/index.php/controller/function/parameters
On with the tutorial!
Step 2 – Install CodeIgniter
CodeIgniter is currently at version 1.7.0, this is a stable release and is the version I use in this tutorial. To get 1.7.0, visit codeigniter.com and click on download CodeIgniter, next to the green downward facing arrow.
Once you have downloaded the .zip folder, extract the contents of the folder into a web directory.
So now you have CodeIgniter installed on your system, it’s time to make a few changes.
Step 3 – A Few Tweaks
We now have a fully working version of CodeIgniter up and running either locally or on our web host. We’re now going to make a few changes to the system: we are going to move the application directory, and update our site URL.
Moving the application directory
This step isn’t necessary, but it helps when upgrading to a new version of CodeIgniter. What we need to do is go into our system/ directory and move the application/ directory out of there and into the base.
Thanks to the genius behind CodeIgniter, we don’t need to update any config file for this change. So that’s it! We’ve moved our application directory!
Updating the base URL
In this step, we’ll edit our first config file. Open application/config/config.php with your favourite text editor. I’m using TextMate. You will need to edit line 14 to be the URL of your CodeIgniter installation, in my case it’s http://localhost/codeigniter/. Remember, you need to add a trailing slash!!
Step 4 – The Welcome Controller
The Welcome Controller is a sign that our CodeIgniter install is working correctly. Simply go to your Codeigniter base URL (the one you changed in the config.php file) and you should see the welcome controller.
If your page is blank, you may have misspelled your base URL, check this and try again.
Step 5 – The File Structure
Up until now we haven’t really properly looked at CodeIgniter, we’ve just set up the base install and made a few changes to make our life easier down the line. Now it’s time to really get started. Let’s look at the file structure for the application/ directory.
- The config folder holds all of our configuration files. We have already bee here to edit our main config.php file, but it also holds things like the autoload file, routes file and database details file.
- The controllers folder holds all of our controllers. Controllers are the main backbone of the application and call upon Models and output Views to the browser. You can store controllers here in sub folders.
- The errors folder holds the error templates that CodeIgniter throws when an error is found. The files inside here can be updated to look more like your application. For the purpose of this tutorial, I won’t be edited these files.
- The helpers folder holds all of the helpers you make. Helpers are a collection of functions that help you do simpler tasks that doesn’t warrant the use of a library.
- The hooks folder holds all the hooks you create. Hooks are designed to alter the system core at different points in the loading process. Hooks are a more advanced way of editing the system core than just extending libraries and are used in different ways depending on different applications. In most cases they are not needed.
- The language folder is a place to store the language files for your site.
- The libraries folder is there to hold all the custom libraries you use. Custom libraries vary from extended core libraries to completely new libraries to offer new functionality such as User Authentication, Templating systems and even CRUD libraries. You can store libraries here in sub folders.
- The models folder holds all of your models for interacting with the database. You can store models here in sub folders.
- The views folder will hold all of your view files. You can store views here in sub folders.
Step 6 – The Controller
Now you know all about CodeIgniter, we can go ahead and make ourselves a controller. Create a file called hello.php in our controllers folder. Here’s the top end of my application directory.
Next we get to add the code to our controller. Take a look at the code below.
01.<?php
02.
03.class Hello extends Controller
04.{
05. function Hello()
06. {
07. parent::Controller();
08. }
09.
10. function index()
11. {
12. $this->load->view('hello');
13. }
14.}
15.
16.
17.
As you can see our code begins with a class declaration. All controller classes in CodeIgniter must begin with an uppercase letter and extend to controller class, this enables us to use the CodeIgniter classes and objects. Our constructor function is the same name as our class, this makes our application PHP4 compatible – just like CodeIgniter! (Note: You don’t have to do it this way, I do both out of habit.)
The next function we have is the index function. The index function is the default function that CodeIgniter will turn to when accessing a controller. Taking this controller as an example, when you go to http://www.yourwebsite.ext/ci_dir/index.php/hello, the index function of the Hello class will be executed. The index function uses the powerful CodeIgniter loader class to load a view file. View files mostly will have a .php extension, so there is no need to put the extension in the first parameter of $this->load->view(), unless you use something other than .php.
Step 7 – The View
So our application is almost done, all we have left to do is to create our view file. So make a new view file called hello.php in our views directory. Your full directory tree should look like mine below.
Ok so as we found out earlier on our view files are the thing the user sees. So in this step we’ll just add some HTML to our file.
01.<html>
02.<head>
03. <title>My First CodeIgniter App - Hello World!</title>
04.</head>
05.<body>
06.
07. <p>Hello, World!</p>
08.
09.</body>
10.</html>
Conclusion
This tutorial gave you an overview of CodeIgniter, an overview of the MVC design pattern, how to install CodeIgniter on your server, a few tweaks to make your life easier, and an example Hello World application. You should be a fully fledged CodeIgniter in no time.