CodeIgniter is a PHP framework, that will speed up the time it takes to build fully featured websites.

I’ve only recently started using it, and here are some tips to getting started.

I’m not going to get into databases, or models, in this tutorial, instead I’m going to focus on creating static pages. In a future article I’m hoping to introduce using a database as well as an XML based database for storing a products list.

Links

This is a list of sites that I visited when I was first starting to learn CodeIgniter.

note: I’ll keep adding to this list as I go through my bookmarks

Installing CodeIgniter

The first thing you’re going to want to do is download CodeIgniter. I’m currently using version 1.7.2.

Once you have downloaded the file, unzip it somewhere you can easily find it, like, oh I don’t know, your desktop.

Inside the file system you will find:

You can go ahead and rename the folder from CodeIgniter_1.7.2 to ci. The next step should be to move the folder to your localhost server, but I found a great tip from one of the tutorials to move the application folder into the root from the system folder.

Now we can move the folder into our localhost.

Setting up CodeIgniter

Run the page now, it should take you to the Welcome Message page.

Before we get into actually building our own CI site, we need to explore the MVC model.

MVC

MVC stands for Model View Controller. MVC is a coding architecture used in software engineering.

Model is the raw data. The model contains all of the database functions. For example: retrieving, inserting, and updating information.

View represents the information the user sees. These are the html pages that make up your site.

The Controller is the logic between the user and your website. So as the user navigates around your site, the Controller is telling CodeIgniter which view to serve and what data will be displayed.

Folder Architecture

Inside of the application folder is where you will spend most of your development time, as this is where the php files for your site will be stored.

There are folders for each: models, views, and controllers (I’ll explore the other folders and their contents as we move along). In the folders, you will find an index.html file, this is just to deny direct access to your folders and should be left alone. If you have already ran CodeIgniter, the page you should see is the Welcome Message, which page can be found inside the views folder as welcome_message.php.

Inside of the controllers folder, you will find welcome.php this is currently the default controller, and it is telling CodeIgniter to display welcome_message.php.

Views

To begin, create a new file inside of your views folder called home.php. This will serve as the landing page for the site.

For now, Let’s just put a h1 tag on the page, with the text Home. Next we will need to tell CI that this is now our default landing page.

<h1>Home</h1>

Controller

I followed a tutorial that named the file the first php file site.php, seemed fitting so I stuck with it as a convention when building my own site.

If you haven’t already done so, create a new site.php file and add the following code.

<?php
class Site extends Controller {
	function Site(){
		parent::Controller();
	}

	function index(){
		$this->load->view('home');
	}
}
?>

The class name must match your file name.

The line function index() is the landing page. $this->load->view('home'); tells CI which view to serve. In this instance the view that CI will be looking for is home.php.

If you run the refresh you browser, nothing is going to change. We still need to tell CI that site.php is the default controller.

Routes

Inside your application folder, open the file called routes.php go down to line 43.

$route['default_controller'] = "welcome";

Change to:

$route['default_controller'] = "site";

Save and close that file.

Now if you refresh you page, you should see “Home”.

That was the very first step in using CI, there is still a lot to do. Next Step: url architecture, and adding pages.

Update: Feb 18 2011 – I haven’t had a chance to really get back to using CodeIgniter since I first wrote this. Not over sure what the status of the next part will be.