Creating CRUD Operations in CodeIgniter 4

 Creating CRUD Operations in CodeIgniter 4


Introduction to CRUD in CodeIgniter 4


CRUD (Create, Read, Update, Delete) operations are fundamental in web development, allowing applications to interact with databases efficiently. CodeIgniter 4, a powerful PHP framework, simplifies CRUD implementation with its built-in features and MVC architecture. This guide provides a step-by-step approach to building a CRUD application in CodeIgniter 4, including role-based access control.


Setting Up CodeIgniter 4


Before implementing CRUD, ensure CodeIgniter 4 is installed. Download it from the official website or use Composer:


```bash composer create-project codeigniter4/appstarter project-name ```


Navigate to the project directory and start the development server:


```bash php spark serve ```


Database Configuration


Configure the database in `app/Config/Database.php`. Set the hostname, username, password, and database name:


```php public $default = [ 'DSN' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'codeigniter_crud', 'DBDriver' => 'MySQLi', 'DBPrefix' => '', 'pConnect' => false, 'DBDebug' => true, // Other configurations... ]; ```


Create a table for the CRUD operations. For example, a `users` table:


```sql CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), email VARCHAR(100), role VARCHAR(50) ); ```


Creating a Model


Models in CodeIgniter 4 handle database interactions. Create a `UserModel.php` in `app/Models`:


```php


class UserModel extends Model { protected $table = 'users'; protected $primaryKey = 'id'; protected $allowedFields = ['name', 'email', 'role']; } ```


Building the Controller


Controllers manage application logic. Create `UserController.php` in `app/Controllers`:


```php


class UserController extends BaseController { public function index() { $model = new UserModel(); $data['users'] = $model->findAll(); return view('users/list', $data); }


public function create() { return view('users/create'); }


public function store() { $model = new UserModel(); $data = [ 'name' => $this->request->getPost('name'), 'email' => $this->request->getPost('email'), 'role' => $this->request->getPost('role') ]; $model->insert($data); return redirect()->to('/users'); }


public function edit($id) { $model = new UserModel(); $data['user'] = $model->find($id); return view('users/edit', $data); }


public function update($id) { $model = new UserModel(); $data = [ 'name' => $this->request->getPost('name'), 'email' => $this->request->getPost('email'), 'role' => $this->request->getPost('role') ]; $model->update($id, $data); return redirect()->to('/users'); }


public function delete($id) { $model = new UserModel(); $model->delete($id); return redirect()->to('/users'); } } ```


Designing Views


Views display data to users. Create the following files in `app/Views/users`:


List Users (`list.php`)


```php


Users

Add User

ID Name Email Role Actions

Edit Delete
```
-- Create User (`create.php`)

```php

Add New User

Name: 
Email: 

Role:  Admin

Save
```
-- Edit User (`edit.php`)

```php

Edit User


Name:  <?= $user['name'] ?>

Email:  <?= $user['email'] ?>

Role:  > Admin

Update
```

Implementing Role-Based Access Control


To restrict access, modify the controller to check user roles. For example:


```php public function index() { if (session()->get('role') != 'admin') { return redirect()->to('/login'); } $model = new UserModel(); $data['users'] = $model->findAll(); return view('users/list', $data); } ```


Routing


Define routes in `app/Config/Routes.php`:


```php $routes->get('/users', 'UserController::index'); $routes->get('/users/create', 'UserController::create'); $routes->post('/users/store', 'UserController::store'); $routes->get('/users/edit/(:num)', 'UserController::edit/$1'); $routes->post('/users/update/(:num)', 'UserController::update/$1'); $routes->get('/users/delete/(:num)', 'UserController::delete/$1'); ```


Conclusion


This guide demonstrated how to implement CRUD operations in CodeIgniter 4, including role-based access control. By following these steps, developers can efficiently manage database interactions while ensuring secure and structured application development. The flexibility of CodeIgniter 4 makes it an excellent choice for building scalable web applications

Ditulis Oleh : Satelit.Net

Judul Artikel "Creating CRUD Operations in CodeIgniter 4" Jika salin Artikel Creating CRUD Operations in CodeIgniter 4, mohon mencantumkan sumber link https://www.satelit.net/2025/11/creating-crud-operations-in-codeigniter.html. Salam from Satelit Web Hosting

:: Web Hosting Indonesia ::

 
 
Terdaftar di WHTop.com
Copyright © Unmetered Hosting