Tutorial lengkap membuat REST API dengan framework CodeIgniter 3. Dari setup hingga testing endpoint.
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.
Sebelum memulai, pastikan Anda sudah menginstall:
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/
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',
);
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);
}
}
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');
}
}
Setelah semua siap, jalankan XAMPP dan test endpoint berikut:
http://localhost/api/products - Ambil semua produkhttp://localhost/api/products - Buat produk baruhttp://localhost/api/products/1 - Update produkhttp://localhost/api/products/1 - Hapus produkMembuat 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.
Belum ada komentar. Jadilah yang pertama!