Introduction to CodeIgniter: Building Your First Application

odeIgniter is a powerful PHP framework designed for developers who need a simple and elegant toolkit to create full-featured web applications. It is known for its speed and performance, making it an ideal choice for both beginners and seasoned developers. In this guide, we will walk you through the steps to build your first application using CodeIgniter, covering the essential concepts and practices.

1. Setting Up CodeIgniter  

Before we dive into building an application, let's set up CodeIgniter on your development environment.

  • Download CodeIgniter: Visit the  official CodeIgniter website and download the latest version.
  • Extract and Install: Extract the downloaded file to your web server's root directory. Rename the folder to your desired application name.
  • Configuration: Open the  application/config/config.php file and set the base URL:
$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'your_database_name',
    '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
);

2. Creating Your First Controller  

Controllers are the heart of CodeIgniter applications, directing traffic and controlling the flow of the application.

  • Create a Controller: 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');
        }
    }
    
  • Access the Controller: Open your browser and navigate to  http://localhost/your_application_name/index.php/welcome .

3. Building a Simple View  

Views handle the presentation layer of your application. They are used to display the output to the user.

  • Create a View: Create a file named  welcome_message.php in the  application/views directory:
<html>
<head>
<title>
Welcome to CodeIgniter
</title>
</head>
<body>
<h1><?php echo 'Hello, World!'; ?></h1>
</body>
</html>
	
	
  • Load the View: The view is already loaded in the  Welcome controller we created earlier. When you access the controller, the view will be displayed.

4. Working with Models  

Models handle data operations such as retrieving, inserting, and updating records in the database.

  • Create a Model: Create a file named  User_model.php in the  application/models directory:
<?php
class User_model extends CI_Model 
{
public
function __construct() 
{
$this
->load->
database
();
    }
public
function get_users() 
{
$query
 = 
$this
->db->
get
(
'users'
);
return
$query
->
result_array
();
    }
  • Use the Model: Load the model in your controller and retrieve data:
public
function index() 
{
$this
->load->
model
(
'User_model'
);
$data
[
'users'
] = 
$this
->User_model->
get_users
();
$this
->load->
view
(
'user_view'
, 
$data
);
}
  • Create a View for Displaying Data: Create a file named  user_view.php in the  application/views directory:
<html>
<head>
    <title>User List</title>
</head>
<body>
    <h1>User List</h1>
    <ul>
<?php
foreach
 (
$users
as
$user
): 
?>
            <li>
<?php
echo
$user
[
'name'
]; 
?>
</li>
<?php
endforeach
; 
?>
    </ul>
</body>
</html>

5. Enhancing Your Application with Libraries and Helpers  

CodeIgniter comes with a rich set of libraries and helpers to extend your application's functionality.

  • Using Libraries: Libraries in CodeIgniter are classes located in the  system/libraries directory or in the  application/libraries directory. For example, to load the email library:
$this
->load->
library
(
'email'
);
	
	
  • Using Helpers: Helpers are groups of related functions located in the  system/helpers directory or in the  application/helpers directory. For example, to load the URL helper:
$this->load->helper('url');

By leveraging these resources, you can add complex functionalities with minimal effort.