Backend Aplikasi Catatan Harian

Belajar membuat backend menggunakan CI (CodeIgniter). Sebelum mulai silahkan baca terlebih dahulu artikel Aplikasi Web dan Android “Catatan Harian” dengan Flutter.

Ada 4 Enpoint yang akan kita pelajari cara membuatnya :

  • GET : https://demo.belajaraplikasi.com/backend-ci/daftarcatatan
  • POST : https://demo.belajaraplikasi.com/backend-ci/tambahcatatan
  • POST : https://demo.belajaraplikasi.com/backend-ci/ubahcatatan
  • GET : https://demo.belajaraplikasi.com/backend-ci/hapuscatatan
  1. Endoint GET Daftar Catatan

Buat file controller “Daftarcatatan.php”

<?php

require (APPPATH.'/libraries/REST_Controller.php');
use Restserver\Libraries\REST_Controller;

class Daftarcatatan extends REST_Controller
{
    public function __construct()
    {
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
        $method = $_SERVER['REQUEST_METHOD'];
        if ($method == "OPTIONS") {
            die();
        }
        parent::__construct();
        $this->load->model('Mod_catatan');
    }

    public function index_get()
    {
        $response = $this->Mod_catatan->daftarCatatan();
        $this->response($response);
    }
}
?>

2. Endpoint POST Tambah Catatan

Buat file controller “Tambahcatatan.php”

<?php

require (APPPATH.'/libraries/REST_Controller.php');
use Restserver\Libraries\REST_Controller;

class Tambahcatatan extends REST_Controller
{
    public function __construct()
    {
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
        $method = $_SERVER['REQUEST_METHOD'];
        if ($method == "OPTIONS") {
            die();
        }
        parent::__construct();
        $this->load->model('Mod_catatan');
    }

    public function index_post()
    {
        $response = $this->Mod_catatan->tambahCatatan();
        $this->response($response);
    }
}
?>

3. Endoint POST Ubah Catatan

Buat file controller “Ubahcatatan.php”

<?php

require (APPPATH.'/libraries/REST_Controller.php');
use Restserver\Libraries\REST_Controller;

class Ubahcatatan extends REST_Controller
{
    public function __construct()
    {
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
        $method = $_SERVER['REQUEST_METHOD'];
        if ($method == "OPTIONS") {
            die();
        }
        parent::__construct();
        $this->load->model('Mod_catatan');
    }

    public function index_post()
    {
        $response = $this->Mod_catatan->ubahCatatan();
        $this->response($response);
    }
}
?>

4. Endpoint GET Hapus Catatan

Buat file controller “Hapuscatatan.php”

<?php

require (APPPATH.'/libraries/REST_Controller.php');
use Restserver\Libraries\REST_Controller;

class Hapuscatatan extends REST_Controller
{
    public function __construct()
    {
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
        $method = $_SERVER['REQUEST_METHOD'];
        if ($method == "OPTIONS") {
            die();
        }
        parent::__construct();
        $this->load->model('Mod_catatan');
    }

    public function index_get()
    {
        $response = $this->Mod_catatan->hapusCatatan();
        $this->response($response);
    }
}
?>

Coba menggunakan REST Client

Daftar Catatan

Daftar Catatan

Tambah Catatan

Tambah Catatan

Ubah Catatan

Ubah Catatan

Hapus Catatan

Hapus Catatan

Source code : https://github.com/handoyoapp/backend-ci

Terima kasih, selamat mencoba

Happy Coding 🙂

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *