Mastering CodeIgniter Framework: Fast, Lightweight Web Development
Course Title: Mastering CodeIgniter Framework: Fast, Lightweight Web Development
Section Title: Routing, Controllers, and Views in CodeIgniter
Topic: Create a basic CodeIgniter application with dynamic routes, controllers, and views.
Course Objective: By the end of this topic, you will be able to:
- Understand the concept of routing in CodeIgniter
- Set up a basic CRUD (Create, Read, Update, Delete) application using dynamic routes, controllers, and views
- Use CodeIgniter's routing system to map URLs to specific controllers and methods
- Create a basic CRUD application with user input validation and error handling
Getting Started
In the previous topic, you learned about the basics of CodeIgniter's MVC architecture, routing system, controllers, and views. Now, it's time to apply these concepts to build a basic web application.
Step 1: Create a New CodeIgniter Project
Create a new CodeIgniter project using the following command in your terminal:
composer create-project codeigniter4/app -b mysql
This will create a basic CodeIgniter project with a MySQL database, PHP version 7.2, and Composer managed dependencies.
Step 2: Define the Route
In CodeIgniter, routes are defined in the app/Config/Router.php
file. This file maps the URL to a specific controller and method. Let's create a simple route for our CRUD application:
// app/Config/Router.php
$routes->get('/', 'Welcome::index');
$routes->get('/users', 'Users::index');
$routes->get('/users/create', 'Users::create');
$routes->get('/users/:id', 'Users::show');
$routes->get('/users/:id/edit', 'Users::edit');
$routes->post('/users', 'Users::create');
$routes->put('/users/:id', 'Users::update');
$routes->delete('/users/:id', 'Users::delete');
This route maps the following URLs to specific controllers and methods:
/
: The welcome page/users
: Displays a list of all users/users/create
: Creates a new user/users/:id
: Displays a single user with the ID:id
/users/:id/edit
: Allows editing a single user with the ID:id
/users
: Creates a new user/users/:id
: Updates a single user with the ID:id
/users/:id
: Deletes a single user with the ID:id
Step 3: Create the Controllers
Create a new controller for the Users
model:
// app/Controllers/Users.php
namespace App\Controllers;
use CodeIgniter\Controller;
class Users extends Controller
{
public function index()
{
// Display a list of all users
$data['title'] = 'Users List';
$this->view->render('users/index', $data);
}
public function create()
{
// Display a form to create a new user
$this->form->initialize([
'action' => 'users',
'method' => 'post',
'rules' => [
'name' => [
'rules' => 'required| postseason minimum_length[3]|valid_email'
],
'email' => [
'rules' => 'required| postseason minimum_length[3]|valid_email'
],
'password' => [
'rules' => 'required| postseason minimum_length[3]'
]
]
]);
$this->view->render('users/create', ['form' => $this->form]);
}
public function show($id)
{
// Display a single user with the ID :id
$data['title'] = 'User Details';
$data['user'] = $this->db->where('id', $id)->firstrow('users');
$this->view->render('users/show', $data);
}
public function edit($id)
{
// Display a form to edit a single user with the ID :id
$data['title'] = 'Edit User: ' . $id;
$data['user'] = $this->db->where('id', $id)->firstrow('users');
$this->form->initialize([
'action' => 'users/put/' . $id,
'method' => 'post',
'rules' => [
'name' => [
'rules' => 'required| postseason minimum_length[3]|valid_email'
],
'email' => [
'rules' => 'required| postseason minimum_length[3]|valid_email'
],
'password' => [
'rules' => 'required| postseason minimum_length[3]'
]
]
]);
$this->view->render('users/edit', ['form' => $this->form, 'user' => $data['user']]);
}
public function update($id)
{
// Update a single user with the ID :id
$id = $this->input->Post('id');
$this->db->where('id', $id)->update('users', [
'name' => $this->input->Post('name'),
'email' => $this->input->Post('email'),
'password' => $this->input->Post('password')
]);
session()->setFlashdata('success', 'User updated successfully!');
return redirect()->to('/users/' . $id);
}
public function delete($id)
{
// Delete a single user with the ID :id
$this->db->where('id', $id)->delete('users');
session()->setFlashdata('success', 'User deleted successfully!');
return redirect()->to('/users');
}
}
Step 4: Create the Views
Create a new file users/index.php
in the app/Views/Users/
directory:
<!-- app/Views/Users/index.php -->
<h1><?php echo $title; ?></h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php foreach ($data['users'] as $user) { ?>
<tr>
<td><?php echo $user->id; ?></td>
<td><?php echo $user->name; ?></td>
<td><?php echo $user->email; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Create a new file users/create.php
in the app/Views/Users/
directory:
<!-- app/Views/Users/create.php -->
<h1><?php echo $title; ?></h1>
<form action="<?php echo site_url('users'); ?>" method="post">
<label for="name">Name:</label>
<input type="text" name="name" required>
<br>
<label for="email">Email:</label>
<input type="email" name="email" required>
<br>
<label for="password">Password:</label>
<input type="password" name="password" required>
<br>
<input type="submit" value="Submit">
</form>
Create a new file users/show.php
in the app/Views/Users/
directory:
<!-- app/Views/Users/show.php -->
<h1><?php echo $title; ?></h1>
<p>ID: <?php echo $user->id; ?></p>
<p>Name: <?php echo $user->name; ?></p>
<p>Email: <?php echo $user->email; ?></p>
Create a new file users/edit.php
in the app/Views/Users/
directory:
<!-- app/Views/Users/edit.php -->
<h1><?php echo $title; ?></h1>
<form action="<?php echo site_url('users/put/' . $id); ?>" method="post">
<label for="name">Name:</label>
<input type="text" name="name" value="<?php echo $user->name; ?>" required>
<br>
<label for="email">Email:</label>
<input type="email" name="email" value="<?php echo $user->email; ?>" required>
<br>
<label for="password">Password:</label>
<input type="password" name="password" required>
<br>
<input type="submit" value="Submit">
</form>
That's it! You now have a basic CRUD application with dynamic routes, controllers, and views. You can test it by running vendor_ package
as Windows in the command mimic thereforeyou keto joined health Time.
Exercise:
- Add a new route for a login page.
- Create a new controller for the login functionality.
- Create a new view for the login page.
- Implement the login functionality to authenticate users.
- Add a new route for a dashboard page.
- Create a new controller for the dashboard functionality.
- Create a new view for the dashboard page.
Leave a comment or ask for help if you need assistance with any of the exercises. I will provide guidance and support to ensure you successfully complete the lab topic.
Images

Comments