<? echo

Project Statuses

Mental Fusion - 85%
FusionBB - 2%
This webpage is built with W3C compliant HTML5.
This webpage is built with W3C compliant CSS3.
Download: Fast, Fun, Awesome
Opera, the fastest and most secure web browser

Example Information

Intro to CodeIgniter

Posted: February 17, 2012 2:28 PM CST by Michael
I've recently been given the opportunity to work with CodeIgniter. What is CodeIgniter? CodeIgniter is an application development platform that uses the MVC framework to deliver its content. MVC stands for "model, view, controller." Essentially, you use models (PHP Classes) to deliver content to "controllers" which then use that content to deliver data to "views" (which is a fancy word for webpage).

How does this work? Let's do a basic example and see what we can learn. First, you have the base application: CodeIgniter. I worked with version 2.1.0 for this example. Simply download and extract CodeIgniter wherever you'd like.

My installation of CodeIgniter is located here: http://mentalfusion.org/CodeIgniter. You do not have to name the directory CodeIgniter. Now, if you clicked that link, you may have seen a page that looks oh-so-similar to my homepage. That's not a coincidence as I recreated my entire homepage in CodeIgniter to demonstrate what CodeIgniter is and can do.

So what do we do after extracting CodeIgniter? It's important to note that all relevant code for the application (or website) you're building is in the "application" directory in CodeIgniter. You shouldn't have to mess with anything outside of that directory.

So what's in the application directory?

drwxr-xr-x 2 4096 2011-11-22 09:52 cache
drwxr-xr-x 2 4096 2012-02-17 11:58 config
drwxr-xr-x 2 4096 2012-02-17 12:22 controllers
drwxr-xr-x 2 4096 2011-11-22 09:52 core
drwxr-xr-x 2 4096 2011-11-22 10:14 errors
drwxr-xr-x 2 4096 2011-11-22 09:52 helpers
drwxr-xr-x 2 4096 2011-11-22 09:52 hooks
drwxr-xr-x 3 4096 2011-11-22 09:52 language
drwxr-xr-x 2 4096 2011-11-22 09:52 libraries
drwxr-xr-x 2 4096 2011-11-22 09:52 logs
drwxr-xr-x 2 4096 2011-11-22 09:52 models
drwxr-xr-x 2 4096 2011-11-22 09:52 third_party
drwxr-xr-x 2 4096 2012-02-17 12:26 views

Above is a list of all directories within the "application" directory. It's very straight forward: all classes (a.k.a., models) go in the "models" directory; all controllers go in the "controllers" directory; and all views go in the "views" directory. So simple.

To render pages, CodeIgniter uses the following structure for URIs: [BASE_PATH]/index.php/[CONTROLLER_NAME]/[FUNCTION_NAME]

If no [CONTROLLER_NAME] is specified, the default controller that is defined in "config/routes.php" is used instead. By default, "welcome" is the route that is used.

Let's change that:

In "applications/config/routes.php" Find:

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

Replace 'welcome' with your custom controller ('mfusion' for this example):

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

Excellent! Now that the default route has been set, we need to create a controller for that route. In "applicaton/controllers/" create a new file called "mfusion.php." Note: It's important that the filename matches the name specified in the "application/config/routes.php" file.

Within the "mfusion.php" file, we need to create a PHP class. The class must be named the same as the file name, but with the first letter capitalized. So we start with:

<?php

// define Mfusion class; must extend the CI_Controller class:
class Mfusion extends CI_Controller {

}

?>

Above, we've declared our "Mfusion" class and we have it extending the "CI_Controller" class. This is extremely important as it affords us access to all of the functions within the main controller object (including our database object, which we'll get to later).

Now, we need to create a view for this controller. So create a new file in "application/views/ called "index_view.php." Note that this can also be a static HTML file, or any other file your server environment supports. For now, we'll leave index_view.php blank.

Heading back to our controller class, we need to create an "index" function. This function is the primary function that all controllers will use to render their information. For this example, we'll just use the index function to render our "index_view.php" file. So your "mfusion.php" file should look something like this:

<?php

// define Mfusion class; must extend the CI_Controller class:
class Mfusion extends CI_Controller {

public function index() {
$this->load->view('index_view'); // Note: the file extension is optional
}
}

?>

$this->load->view('<file>') will pull the file specified from the "applications/views/" directory and render its contents. Now we essentially have a working page that will no longer provide a 404 error, but it will be blank as our "index_view.php" file is empty.

Let's try passing a variable to "index_view.php" so it can be printed. To do that, all we have to do is create an array of items to pass to the view. Afterwards, pass that array as an argument in the $this->load->view() function:

<?php

// define Mfusion class; must extend the CI_Controller class:
class Mfusion extends CI_Controller {

public function index() {
$data = array(
'hello' => 'Hello, World!'
);

$this->load->view('index_view',$data); // Note: the file extension is optional
}
}

?>

The $data array, as you can see, contains only one item with the key 'hello.' It's important to note that when referenced in the view file, you would call $hello instead of $data['hello'] or $data->hello.

So let's print that variable in our "index_view.php" file:

<?php
echo $hello; // prints "Hello, World!"
?>

So that's the gist of how to build a page in CodeIgniter. It's quite straight forward when you get down to it.

Let's look a bit at how to get database connections going in CodeIgniter. First, set up your database variables in the "applications/config/database.php file:

$db['default']['hostname'] = 'localhost'; // database hostname
$db['default']['username'] = 'username'; // database username
$db['default']['password'] = 'abc12345'; // database password
$db['default']['database'] = 'db_name'; // database name
$db['default']['dbdriver'] = 'mysql'; // one of the following: mysql, mysqli, postgre, odbc, mssql, sqlite, oci8

After you set your database variables in the above file, head to "applications/config/autoload.php" and find the following line:

$autoload['libraries'] = array();

Change it to pass the value 'database' in the array:

$autoload['libraries'] = array('database');

Now, the database library will be autoloaded into any controller you create and, if your database settings are correct, reloading your page will not show any error.

To use the database object, you just have to call $this->db->[function]. Below is a couple of examples on usage:

$this->db->query('<query>'); // This is the general query function
$this->db->get('table_name'); // This is the same as calling $this->db->query('SELECT * FROM table_name')

$results = $this->db->results(); // This will make the object $results that contains the results of the query. To access the information
// call each record like so: $results->[ROW_INDEX]->columnname
// Alternatively, in a foreach:
// foreach ($results as $row):
// echo $row->columnname; // Where columnname is the name of the column you wish to use
// endforeach;

Now that we've established database connectivity, we can easily manipulate our tables or database without having to worry too much about consistency between database calls.

In conclusion, CodeIgniter is fairly nice as an application deployment framework. It takes a lot of the work out of having to build a factory framework yourself and allows for easily packaged and deployed software to be developed and distributed in a reasonable fashion.

I may post another article on CodeIgniter models, but for now, this is a good start on the topic I think.
; ?>