Apakah itu Rest API ? Rest API (Application Programming Interface) atau sering juga disebut dengan istilah API saja merupakan penghubung antara front–end dan back–end aplikasi. Front–end sendiri aplikasi interface yang digunakan user atau pengguna aplikasi. Contoh dari front-end aplikasi yaitu aplikasi android dan iOS. Back–end aplikasi yang digunakan administrator untuk manajemen data. Sehingga dengan Rest API data dari front-end ke back-end ataupun sebaliknya dapat diolah seperti menampilkan, menambahkan, mengubah dan menghapus data.
Apa saja yang perlu dipersiapkan ?
– Download Framework CodeIgniter 3.x klik disini
– Download MySQL (untuk database)
– PHP Version 7.x
– Download Library CodeIgniter RestServer klik disini
Konfigurasi CodeIginiter dengan Library RestServer
- Masuk ke project CodeIginiter yang sudah dibuat
Masuk folder application/config
– Tambahkan file Idap.php dan rest.php
Masuk folder application/helpers
– Tambahkan file db_helper.php
Masuk folder application/language/english
– Tambahkan file rest_controller_lang.php
Masuk folder application/libraries
– Tambahkan file Format.php dan REST_Controller.php
Konfigurasi CodeIginiter dengan Database MySQL
- Masih di folder application
- Masuk ke folder config
- Buka file database.php
- Ubah pengaturan $db[‘default’] menjadi seperti berikut :
‘hostname’ => ‘localhost:3306’,
‘username’ => ‘root’, (disesuaikan sendiri)
‘password’ => ”, (disesuaikan sendiri)
‘database’ => ‘nama_database’, (disesuaikan sendiri)
‘dbdriver’ => ‘mysqli’,
- Masuk ke folder config
- Buka file autoload.php
ubah code : $autoload[‘libraries’] = array(‘database’);
- Masuk ke folder config
- Buka file config.php
ubah code : $config[‘base_url’] = ‘http://127.0.0.1:8000’;
- Masuk ke folder controllers
- Buat file get_produk.php
- Tuliskan code seperti berikut :
<?php require (APPPATH.'/libraries/REST_Controller.php'); use Restserver\Libraries\REST_Controller; class Get_product extends REST_Controller{ public function __construct(){ parent::__construct(); $this->load->model('mod_product'); } public function index_get(){ $response = $this->mod_product->get_product(); $this->response($response); } } ?>
- Masuk ke folder models
- Buat file mod_product.php
- Tuliskan code seperti berikut :
<?php Class Mod_product extends CI_Model { public function get_product() { $this->db->order_by('catalog_id', 'DESC'); $query = $this->db->get('catalog'); if ($query->num_rows() > 0) { $response = array(); $response['error'] = false; $response['message'] = 'Success get product'; foreach ($query->result() as $row) { $harga = $row->catalog_harga; $tempArray = array(); $tempArray['product_id'] = (int)$row->catalog_id; $tempArray['product_name'] = $row->catalog_nama; $tempArray['price'] = (int)$harga; $tempArray['note'] = (int)$row->catalog_deskripsi; $tempArray['image'] = 'http://127.0.0.1:8000/image/'.$row->catalog_image; $response['data'][] = $tempArray; } return $response; } return false; } }
Sekarang kita bisa mencoba API Get Product yang sudah dibuat menggunakan browser.
Ketikkan base url seperti pada config di browser: http://127.0.0.1:8000, seperti berikut :

Terima kasih
Selamat mencoba
Happy Coding 🙂
I’m a result mobile engineer, as well as deep understanding of digital systems.