LATIHAN FLUTTER WEEK#5 : SIKAR Rest API Codeigniter Membuat Controller

LATIHAN FLUTTER WEEK#5 : SIKAR Rest API Codeigniter Membuat Controller

controller : Karyawan.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . '/libraries/REST_Controller.php';
require APPPATH . '/libraries/php-jwt-master/src/JWT.php';

use \Firebase\JWT\JWT;
use Restserver\Libraries\REST_Controller;

class Karyawan extends REST_Controller {
    
 //INIT
   function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->model('M_Karyawan');
   }


    //GET
   public function index_get(){	   

        //TOKEN JWT
        $key = "meetApFlutterMobileTokoKopiHaqiqiCilebut";
        $web = array( "web" => "http://kailham.com",);
        //ENCODE TOKEN
        $jwt = JWT::encode($web, $key);
        //DECODE TOKEN
        $decoded = JWT::decode($jwt, $key, array('HS256'));

 //GET PARAMETER
 $kriteria = $this->get('kriteria');
        $token    = $this->get('token');
        $data     = "";

 //METHOD
 if($kriteria===null){
           //ALL KARYAWAN
    $data = $this->M_Karyawan->getKaryawan();
 }else{
           //FILTER BY NIK & NAME
    $data = $this->M_Karyawan->getKaryawanByName($kriteria);
 }


    //TOKEN MATCH
        if($jwt==$token){
                //DATA FOUND
                if($data)
                {
                        $this->response([
                                        'status' => True,
                                        'message' => 'Karyawan found',
                                        'token' => $jwt,
                                        'data' => $data,
                                ], REST_Controller::HTTP_OK); 
 
                //DATA NOT FOUND
                }else{
                    $this->response([
                            'status' => False,
                            'message' => 'Karyawan not found',
                        ], REST_Controller::HTTP_NOT_FOUND); 
                }
        }else{
       //INVALID TOKEN
                    $this->response([
                            'status' => False,
                            'message' => 'Invalid Token',
                            'token' => $jwt,
                        ], REST_Controller::HTTP_NOT_FOUND);
        }
   }
 


 
}

 

testing di postman

http://kailham.com/apiSikar/

http://kailham.com/apiSikar/karyawan

invalid token

http://kailham.com/apiSikar/karyawan?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ3ZWIiOiJodHRwOlwvXC9rYWlsaGFtLmNvbSJ9.cGCF3rTk2yvcvL9JYLgQpeONXDRYq5iC0Hj56ENCnI8

dengan token

http://kailham.com/apiSikar/karyawan?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ3ZWIiOiJodHRwOlwvXC9rYWlsaGFtLmNvbSJ9.cGCF3rTk2yvcvL9JYLgQpeONXDRYq5iC0Hj56ENCnI8&kriteria=LT

dengan kriteria

Flutter Developer