Cara Membuat REST API dengan CodeIgniter 3
Kembali ke Blog

Cara Membuat REST API dengan CodeIgniter 3

Tutorial
Nugroho Setiawan 02 Apr 2026 7 min baca 464 kata 2 views
Tutorial lengkap membuat REST API dengan framework CodeIgniter 3. Dari setup hingga testing endpoint.

Pendahuluan

REST API (Representational State Transfer Application Programming Interface) adalah standar arsitektur untuk komunikasi antar aplikasi melalui HTTP. CodeIgniter 3 adalah framework PHP yang ringan dan cepat, sangat cocok untuk membangun REST API. Dalam tutorial ini, kita akan membuat REST API lengkap dari awal.

Persiapan

Sebelum memulai, pastikan Anda sudah menginstall:

  • PHP 7.x atau lebih baru
  • Composer
  • XAMPP atau LAMP stack
  • Postman untuk testing

Setup Project

Download CodeIgniter 3 dari situs resmi dan extract ke folder htdocs Anda. Kemudian install library REST Server:

// Download REST Server library dari:
// https://github.com/chriskacerguis/codeigniter-restserver
// Copy file REST_Controller.php ke application/libraries/
// Copy file Format.php ke application/libraries/

Konfigurasi Database

Edit file application/config/database.php dan sesuaikan konfigurasi database Anda:

$db['default'] = array(
  'dsn'   => '',
  'hostname' => 'localhost',
  'username' => 'root',
  'password' => '',
  'database' => 'rest_api_db',
  'dbdriver' => 'mysqli',
);

Membuat Controller API

Buat file baru application/controllers/api/Products.php:

require APPPATH . 'libraries/REST_Controller.php';

class Products extends REST_Controller {

  public function __construct() {
    parent::__construct();
    $this->load->model('Product_model');
  }

  // GET all products
  public function index_get() {
    $products = $this->Product_model->get_all();
    $this->response($products, 200);
  }

  // GET single product
  public function index_get($id) {
    $product = $this->Product_model->get_by_id($id);
    if ($product) {
      $this->response($product, 200);
    } else {
      $this->response(['message' => 'Not found'], 404);
    }
  }

  // POST create product
  public function index_post() {
    $data = $this->post();
    $id = $this->Product_model->create($data);
    $this->response(['id' => $id, 'message' => 'Created'], 201);
  }

  // PUT update product
  public function index_put($id) {
    $data = $this->put();
    $this->Product_model->update($id, $data);
    $this->response(['message' => 'Updated'], 200);
  }

  // DELETE product
  public function index_delete($id) {
    $this->Product_model->delete($id);
    $this->response(['message' => 'Deleted'], 200);
  }
}

Membuat Model

Buat file application/models/Product_model.php untuk handle operasi database:

class Product_model extends CI_Model {
  public function get_all() {
    return $this->db->get('products')->result();
  }
  public function get_by_id($id) {
    return $this->db->get_where('products', ['id' => $id])->row();
  }
  public function create($data) {
    $this->db->insert('products', $data);
    return $this->db->insert_id();
  }
  public function update($id, $data) {
    $this->db->where('id', $id)->update('products', $data);
  }
  public function delete($id) {
    $this->db->where('id', $id)->delete('products');
  }
}

Testing dengan Postman

Setelah semua siap, jalankan XAMPP dan test endpoint berikut:

  1. GET http://localhost/api/products - Ambil semua produk
  2. POST http://localhost/api/products - Buat produk baru
  3. PUT http://localhost/api/products/1 - Update produk
  4. DELETE http://localhost/api/products/1 - Hapus produk

Best Practices

  • Selalu gunakan HTTPS di production
  • Implementasikan authentication (API Key atau JWT)
  • Validasi semua input dari client
  • Gunakan proper HTTP status codes
  • Tambahkan rate limiting untuk mencegah abuse

Kesimpulan

Membuat REST API dengan CodeIgniter 3 cukup straightforward. Framework ini menyediakan fondasi yang solid untuk membangun API yang cepat dan reliable. Pastikan Anda mengikuti best practices untuk keamanan dan performa di lingkungan production.

Terakhir diperbarui 05 Apr 2026

Komentar

Komentar ditinjau sebelum tampil.

Belum ada komentar. Jadilah yang pertama!