Cari Halaman

Pengenalan CodeIgniter 4

Sarah Johnson
1 January 0001
4-5 jam

Pengenalan CodeIgniter 4

CodeIgniter adalah framework PHP yang ringan, cepat, dan mudah digunakan. CodeIgniter 4 membawa berbagai improvement termasuk dukungan PHP 8 dan fitur modern.

Apa itu CodeIgniter?

CodeIgniter adalah framework PHP open-source yang mengikuti pola MVC (Model-View-Controller) dan dirancang untuk pengembangan aplikasi web yang cepat dan efisien.

Keunggulan CodeIgniter 4

  • Lightweight: Framework yang ringan dan cepat
  • MVC Pattern: Struktur yang terorganisir
  • Active Record: Database abstraction layer
  • Form Validation: Built-in form validation
  • Security: CSRF protection, XSS filtering
  • Session Management: Flexible session handling
  • Caching: Multiple cache backends
  • CLI Tools: Command-line interface

Arsitektur CodeIgniter 4

CodeIgniter mengikuti pola MVC yang memisahkan logic aplikasi:

Model

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?php

namespace App\Models;

use CodeIgniter\Model;

class UserModel extends Model
{
    protected $table = 'users';
    protected $allowedFields = ['name', 'email', 'password'];
}

View

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<!-- app/Views/users/index.php -->
<!DOCTYPE html>
<html>
<head>
    <title>Daftar Pengguna</title>
</head>
<body>
    <h1>Daftar Pengguna</h1>
    <?php foreach($users as $user): ?>
        <div><?= $user['name'] ?></div>
    <?php endforeach; ?>
</body>
</html>

Controller

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php

namespace App\Controllers;

use App\Models\UserModel;

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

Persiapan Development Environment

System Requirements

  • PHP >= 8.1
  • Composer
  • Web server (Apache/Nginx)
  • Database (MySQL/PostgreSQL/SQLite)

Instalasi CodeIgniter 4

1
2
3
composer create-project codeigniter4/appstarter my-ci4-app
cd my-ci4-app
php spark serve

Struktur Direktori CodeIgniter 4

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
my-ci4-app/
├── app/                 # Application code
   ├── Config/         # Configuration files
   ├── Controllers/    # Controller classes
   ├── Models/         # Model classes
   └── Views/          # View files
├── public/             # Web server root
├── system/             # Framework core
├── writable/           # Logs, cache, uploads
└── spark              # CLI tool

Fitur Utama CodeIgniter 4

1. Routing

1
2
3
// app/Config/Routes.php
$routes->get('users', 'Users::index');
$routes->post('users', 'Users::create');

2. Database

1
2
3
4
5
6
// Query Builder
$db->table('users')->where('active', 1)->get()->getResult();

// Active Record
$userModel = new UserModel();
$users = $userModel->where('active', 1)->findAll();

3. Form Validation

1
2
3
4
5
6
7
8
$rules = [
    'name' => 'required|min_length[3]',
    'email' => 'required|valid_email'
];

if (!$this->validate($rules)) {
    return view('form', ['errors' => $this->validator->getErrors()]);
}

Next Steps

Di modul selanjutnya, kita akan mempelajari:

  • Setup project CodeIgniter 4
  • Konfigurasi database
  • Membuat controller dan model
  • Form handling dan validation

Mari lanjutkan ke modul berikutnya!

Modul Sebelumnya
Modul Selanjutnya

Progress Seri

Lanjutkan pembelajaran Anda

1/10
Modul
10%
Sedang Berjalan