Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Using CodeIgniter - Comprehensive Tutorial

Introduction

CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications. In this tutorial, we will cover everything you need to get started with CodeIgniter, from installation to building a complete web application.

Installation

First, you need to download the CodeIgniter framework. You can get the latest version from the official website.

Once downloaded, unzip the package and place it in your web server's root directory. Rename the folder to your desired project name.

For example, if you are using XAMPP, place the folder in C:\xampp\htdocs\ and rename it to myproject.

Configuration

Open the application/config/config.php file. Set the base URL to match your server configuration:

$config['base_url'] = 'http://localhost/myproject/';

Next, configure the database settings in application/config/database.php:

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'mydatabase',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);
                

Creating a Controller

Controllers are the heart of your application, as they determine how HTTP requests should be handled. Create a new file named Welcome.php in the application/controllers/ directory:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function index()
    {
        $this->load->view('welcome_message');
    }
}
                

Creating a View

Views are used to display content. Create a new file named welcome_message.php in the application/views/ directory:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Welcome to CodeIgniter</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <p>Welcome to your first CodeIgniter application.</p>
</body>
</html>
                

Loading the View from Controller

In the index method of your Welcome controller, the $this->load->view('welcome_message') line tells CodeIgniter to load the welcome_message view. When you access http://localhost/myproject/index.php/welcome, you should see the "Hello, World!" message.

Creating a Model

Models are used to interact with your database. Create a new file named Welcome_model.php in the application/models/ directory:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome_model extends CI_Model {

    public function get_message()
    {
        return "Hello from the Model!";
    }
}
                

Using the Model in Controller

Modify your Welcome controller to use the model:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function index()
    {
        $this->load->model('Welcome_model');
        $data['message'] = $this->Welcome_model->get_message();
        $this->load->view('welcome_message', $data);
    }
}
                

Update your welcome_message.php view to display the message from the model:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Welcome to CodeIgniter</title>
</head>
<body>
    <h1><?php echo $message; ?></h1>
</body>
</html>
                

Conclusion

Congratulations! You have successfully created a simple CodeIgniter application. This tutorial covered the basics of setting up CodeIgniter, configuring it, and creating a simple MVC (Model-View-Controller) structure. CodeIgniter is a powerful and flexible framework that can help you build robust web applications quickly and efficiently.