Building Applications with CodeIgniter

Tutorial 5 of 5

Building Applications with CodeIgniter

1. Introduction

Goal

This tutorial aims to guide you through the process of building a simple web application using CodeIgniter, a powerful PHP framework with a very small footprint.

Learning Outcome

By the end of this tutorial, you should be able to understand and implement the MVC (Model-View-Controller) architecture, create controllers, views, and build applications with CodeIgniter.

Prerequisites

Before you start, you should have a basic understanding of PHP and MySQL. Familiarity with OOP (Object Oriented Programming) concepts would be beneficial but is not mandatory.

2. Step-by-Step Guide

Installation of CodeIgniter

To start off, download the latest version of CodeIgniter from the official website and extract it into your localhost directory.

MVC Architecture

CodeIgniter uses the MVC (Model-View-Controller) architecture.

  • Model: The part of your application that handles data and database interactions.
  • View: The part of your application that generates the HTML and presents data to the user.
  • Controller: The part of your application that handles user interaction, works with the model, and ultimately selects a view to render.

Creating Controllers

In CodeIgniter, the first step is to create a controller. A controller is simply a class that helps delegate work. It is the stick that stirs the drink.

//Filename: application/controllers/Hello.php
<?php
class Hello extends CI_Controller {
   public function index() {
      echo "Hello, World!";
   }
}

The above controller (Hello.php) has a function index(). When this controller is called from a URL, the index() function is executed by default.

Creating Views

A View is simply a web page, or a page fragment, like a header, footer, sidebar, etc. In other words, views are the pages that the user sees and interacts with.

//Filename: application/views/hello_view.php
<html>
   <head>
      <title>My Simple CodeIgniter App</title>
   </head>
   <body>
      <h2>Hello, World!</h2>
   </body>
</html>

This is a simple view that will display "Hello, World!" when called.

3. Code Examples

Example 1: Loading a View in the Controller

To load a view, we use the $this->load->view() function in our controller.

//Filename: application/controllers/Hello.php
<?php
class Hello extends CI_Controller {
   public function index() {
      $this->load->view('hello_view');
   }
}

When the index() function of the Hello controller is called, it will load hello_view.php.

Example 2: Passing Data to the View

We can pass data to the view by sending an array or an object to the second argument of the $this->load->view() function.

//Filename: application/controllers/Hello.php
<?php
class Hello extends CI_Controller {
   public function index() {
      $data['message'] = 'Hello, World!';
      $this->load->view('hello_view', $data);
   }
}

In the view, we can display this data like so:

//Filename: application/views/hello_view.php
<html>
   <head>
      <title>My Simple CodeIgniter App</title>
   </head>
   <body>
      <h2><?php echo $message; ?></h2>
   </body>
</html>

This will display "Hello, World!".

4. Summary

In this tutorial, we have covered the basics of CodeIgniter including its installation, understanding MVC architecture, creating controllers, and views. The next steps would be to learn about Models and how to interact with a database using CodeIgniter's built-in classes.

5. Practice Exercises

  1. Exercise 1: Create a controller named Welcome that loads a view named welcome_message.
  2. Exercise 2: Modify the Welcome controller to pass a custom welcome message to the welcome_message view. Display this message in the view.
  3. Exercise 3: Create a new controller that loads two different views to create a complete HTML page (for example, a header view and a body view).

Remember, practice is key to mastering any programming language or framework. Happy coding!