Basic Tutorial on Using CodeIgniter

In this tutorial, the author will provide a tutorial on the basic use of the PHP framework, namely CodeIgniter. The author uses CodeIgniter v 2.1.4 which can be downloaded directly from the official CodeIgniter web, namely http://ellislab.com/codeigniter. What you need for this tutorial is:

  1. CodeIgniter 2.1.4
  2. Text Editor (Sublime Text, CodeIgniter, Brackets, etc.)
  3. XAMPP / WAMP

PHP CodeIgniter Framework
PHP CodeIgniter Framework

What is done for the first time is to download CodeIgniter v 2.1.4 which has been provided on the official website, then extract the downloaded results and generate a CodeIgniter folder. The folder must be moved into the htdocs folder in the XAMPP / WAMP directory.

Related Article: Codeigniter Initial Setup

After that, the reader must run the web server on XAMPP / WAMP in order to run the PHP script. If so, the reader can directly open the browser page and then write “localhost/CodeIgniter” in the Browser URL (without the “”), if the “Welcome to CodeIgniter” display appears, it means that you have successfully used the CodeIgniter framework for the first time.

Welcome Page CodeIgniter
Welcome Page CodeIgniter

Before we discuss how this page can appear, readers should know that the CodeIgniter framework has an MVC / Model View Controller structure so that if you look at the contents of the application folder in CodeIgniter that was downloaded earlier there will be a models, views and controllers folder. (For those who do not understand the MVC programming model, you can read here: http://en.wikipedia.org/wiki/Model–view–controller)

Folder Structure in CodeIgniter
Folder Structure in CodeIgniter

Let’s take a look at the routes.php file in the application/config folder, in that folder there is code like this:

[php]$route[‘default_controller’] = “welcome”;[/php]

-> this code shows that the default controller or the first controller to run when running our web application is the welcome.php file which is in the controllers folder. Readers can change the default controller according to their wishes later, be patient, hehehe

From there we already know that when the website is first run (when we type localhost/CodeIgniter) it will run the welcome.php file which is in the application/controllers folder. Inside the welcome.php file contains:

<?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);

class Welcome extends CI_Controller {

/**

  • Index Page for this controller.
    *
  • Maps to the following URL
  • http://example.com/index.php/welcome
  • – or –
  • http://example.com/index.php/welcome/index
  • – or –
  • Since this controller is set as the default controller in
  • config/routes.php, it’s displayed at http://example.com/
    *
  • So any other public methods not prefixed with an underscore will
  • map to /index.php/welcome/
  • @see http://codeigniter.com/user_guide/general/urls.html
    */
    public function index()
    {
    $this->load->view(‘welcome_message’);
    }
    }
    it is the main structure for a controller in CodeIgniter. the class name used is Welcome, this is because it is adjusted to the controller file name, namely welcome.php . If we have a controller with the file name products.php then we have to create a class like this in it:

class Products extends CI_Controller {
public function index()
{
//Do something here…
}
}
The welcome.php file also has an index function, this function is the default function that will be called or executed the first time the controller runs. we can also add other functions in it, like this for example: