RestFull CodeIgniter Example
Setelah berhasil menginstal REst Api pada condeIgniter, sekarang kita akan coba untuk membuat sebuah server dan sebuah client, bagi yang belom download rest codeIgniternya bisa di download di : REst CodeIgniter
Ingat : cURL harus sudah di enable , cara enable cURL tinggal uncomment pada file php.ini di server anda…
damal postingan ini terdapat 2 source code php :
1. untuk rest server
2. untuk rest client
file untuk rest servernya
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | <?php defined('BASEPATH') OR exit('No direct script access allowed'); /** * * @package CodeIgniter * @subpackage Rest Server * @category Rest Controller * @author WisPukako * @link http://www.pukakomedia.net/ */ // This can be removed if you use __autoload() in config.php OR use Modular Extensions require APPPATH.'/libraries/REST_Controller.php'; class Rest_Barang extends REST_Controller { protected $builtInMethods; public function __construct() { parent::__construct(); $this->__getMyMethods(); $this->load->model('m_barang'); } /* ========================================================================================== * name : barangsatu_get = mengambil informasi satu barang berdasarkan id * by : * how to : * -- call function : `barangsatu/id/{id of barang}/format/{json/xml/*} * -- variable : id = id dari barang yang akan dicari * -- output : mengambelikan semua atribut dari barang sesuai dengan format yang diminta */ function a_barang_get() { /* * check apakah atribut id sudah di set saat pemanggilan */ if(!$this->get('id')) { $this->response(NULL, 400); // jika belom diset maka akan dibawa ke page not found } /* * jika sudah diset */ else { /* * mengambil data sesuai dengan id */ $query = $this->m_barang->get_barang_by($this->get('id')); /* * jika query berhasi akan ditampilkan hasilnya */ if($query) { $this->response($query, 200); // 200 being the HTTP response code } else { $this->response(array('error' => 'User could not be found'), 404); } } } /* ========================================================================================== * name : all_barang_get : mengambil semua informasi barang secara lengkap * by : * how to : * -- call function : /all_barang/format/{json/xml/*} * -- variable : - * -- output : mengambelikan semua atribut dari barang sesuai dengan format yang diminta */ function all_barang_get() { /* * query semu data barang dari database */ $query = $this->m_barang->get_all_barang(); /* * jika query berhasil */ if($query) { $this->response($query, 200); // 200 being the HTTP response code } else { $this->response(array('error' => 'User could not be found'), 404); } } /* ========================================================================================== * name : add_barang : menambahkan sebuah barang kedata base * by : * how to : * -- call function : /add_barang/format/{json/xml/*} * -- variable : - * -- output : true or false tergantung kesuksesan proses */ function add_barang_post() { $data = array( 'id' => $this->post('id'), 'nama' => $this->post('nama'), 'hargasewa' => $this->post('hargasewa'), 'satuan' => $this->post('satuan'), 'available' => $this->post('available') ); if($this->get('svaha')==1){ $query = $this->m_barang->insert_new_barang($data); } if($query) { $this->response($query, 200); // 200 being the HTTP response code } else { $this->response($query, 404); // 200 being the HTTP response code } } /* ========================================================================================== * name : update_barang : mengupdate sebuah barang kedata base * by : * how to : * -- call function : /update_barang/id/$idbarang/format/{json/xml/*} * -- variable : - * -- output : true or false tergantung kesuksesan proses */ function update_barang_post() { $data = array( 'nama' => $this->post('nama'), 'hargasewa' => $this->post('hargasewa'), 'satuan' => $this->post('satuan'), 'available' => $this->post('available') ); $id = $this->get('id'); $query = $this->m_barang->update_barang($data, $id); $this->response($query, 200); // 200 being the HTTP response code } /* * del_barang($id) : menghapus baranga dengan id = id */ function del_barang_delete(){ $id = $this->get('id'); $query = $this->m_barang->del_barang($id); echo "<script>alert('Delete Barang Berhasil')</script>"; $this->response($query, 200); // 200 being the HTTP response code } } ?> |
file untuk rest client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * * @package CodeIgniter * @subpackage Rest Client * @category Rest Controller * @author WisPukako * @link http://www.pukakomedia.net/ */ class rest_client_barang extends CI_Controller { public function __construct() { parent::__construct(); // Load the configuration file $this->load->config('rest'); // Load the rest client $this->load->spark('restclient/2.0.0'); //mengeset server rest dari controller ini $this->rest->initialize(array('server' => 'http://localhost/ci-koperasi/index.php/rest_barang/')); } /* * redirect : menampilkan tombel kembali ke 2 halaman sebelumnya */ function redirect(){ $this->load->view('back2page'); } /* * contoh untuk mengambil semua barang dari rest server kita dan kita masukkan ke dalam view */ public function get_all_barang_crud_html() { if($query =$this->rest->get('all_barang/format/json')){ //perbedaan yang ada hanyalah disini, kita menjadikan rest server sebagai model untuk mengambil data $data['barang']=$query; $i = 0; foreach ($query as $as){ $i++; } $data['sum']=$i; } $this->load->view('barang/load_list_barang_crud_rest',$data); } /* * menampilkan form edit */ public function form_edit_barang($id){ $data['data'] = $this->rest->get('a_barang/id/'.$id.'/format/json'); $header['page_title'] = 'Edit barang | CI_Koperasi - 113090044 - 113090075'; $this->load->view('header_pop',$header); $this->load->view('barang/form_edit_barang_html_rest',$data); } /* * memanggil rest dengan metod post, lihat dengan teliti ya... */ public function edit_barang(){ $id = $this->input->post('id'); $data = array( 'nama' => $this->input->post('nama'), 'hargasewa' => $this->input->post('hargasewa'), 'satuan' => $this->input->post('satuan'), 'available' => $this->input->post('available') ); if($query = $this->rest->post('update_barang/id/'.$id.'/format/php',$data)){ echo "<script>alert('Proses edit barang sukses'); window.close ();</script>"; } else { echo "<script>alert('Gagal coy'); window.close ();</script>"; } } /* * del_barang($id) : menghapus baranga dengan id = id */ public function del_barang($id){ $del = $this->rest->delete('del_barang/id/'.$id.'/format/json'); if($del){ echo "<script>alert('Delete Barang Berhasil')</script>"; } $this->redirect(); } /* * form add barang : menampilkan form menambah brang ke database * */ public function form_add_barang(){ $this->load->view('barang/script'); $this->load->view('barang/form_add_barang_rest'); } /* * fungsi untuk menambahkan brang ke data base */ public function add_barang_rest(){ $data = array( 'id' => $this->input->post('id'), 'nama' => $this->input->post('nama'), 'available' => $this->input->post('available'), 'hargasewa' => $this->input->post('hargasewa'), 'satuan' => $this->input->post('satuan') ); $query = $this->rest->post('add_barang/svaha/1/format/php',$data); if($query) { //echo $query; echo "<script>alert('Tambah Barang Berhasil')</script>"; //$this->redirect(); } else { echo "<script>alert('Terjadi Error Saat Query')</script>"; } } } ?> |
cara mengecek fungsi pada server
mengecek sebuah fungsi pada server hampir sama dengan menguji fungsi pada controller biasa,
misal kita akan menguji fungsi “all_barang_get” pada server, maka cara pemanggilannya adalah
http://localhost/namafolderci/index.php/rest_barang{*nama class server}/all_barang{*nama fungsi}/format/json{*format yang diinginkan }