A5, AEHS, Lahore, Pakistan
+92 306 77 57 681
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.
Before we dive into building an application, let's set up CodeIgniter on your development environment.
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
);
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');
}
}
http://localhost/your_application_name/index.php/welcome .Views handle the presentation layer of your application. They are used to display the output to the user.
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>
Welcome controller we created earlier. When you access the controller, the view will be displayed.Models handle data operations such as retrieving, inserting, and updating records in the database.
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
();
}
public
function index()
{
$this
->load->
model
(
'User_model'
);
$data
[
'users'
] =
$this
->User_model->
get_users
();
$this
->load->
view
(
'user_view'
,
$data
);
}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>CodeIgniter comes with a rich set of libraries and helpers to extend your application's functionality.
system/libraries directory or in the application/libraries directory. For example, to load the email library:$this
->load->
library
(
'email'
);
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.