Course Content
Expense Manager App – Login API
0/1
Expense Manager App – Customer API
0/1
Expense Manager App – Product API
0/1
Expense Manager App – Payment Mode API
0/1
CodeIgniter 4 API Tutorial
    About Lesson

    To create an API for managing customers in an Expense Manager App using CodeIgniter 4, you need to set up RESTful routes, controllers, and models. This example will guide you through building a simple API with endpoints for managing customers.

    Steps to Create the Customer API

    1. Database Setup

    Create a customer_info table in your database to store customer information.

    CREATE TABLE `customer_info` (
      `id` bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
      `type` enum('customer','supplier') DEFAULT NULL,
      `name` varchar(100) NOT NULL,
      `name_hi` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
      `mobile_country` varchar(10) NOT NULL,
      `mobile_number` varchar(100) NOT NULL,
      `email` varchar(100) NOT NULL,
      `password` varchar(100) NOT NULL,
      `address` text NOT NULL,
      `image_url` text NOT NULL,
      `account_holder_name` varchar(100) NOT NULL,
      `account_number` varchar(100) NOT NULL,
      `ifsc_code` varchar(100) NOT NULL,
      `bank_name` varchar(100) NOT NULL,
      `city_id` varchar(10) DEFAULT NULL,
      `city_name` varchar(100) DEFAULT NULL,
      `company_name` varchar(100) DEFAULT NULL,
      `ip_address` varchar(100) NOT NULL,
      `created_at` datetime NOT NULL,
      `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
      `status` varchar(10) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

     


    2. Create the Model

    Create a model named CustomerModel.php in the app/Models/customer directory.


    class CustomerModel extends Model {

    protected $db;
    public function __construct(ConnectionInterface &$db) {
    $this->db =& $db;

    $this->table = 'customer_info';
    }

    public function addEntry($data) {
    $this->db
    ->table($this->table)
    ->insert($data);
    return $this->db->insertID();
    }

    public function updateEntry($where, $data) {
    return $this->db
    ->table($this->table)
    ->where($where)
    ->set($data)
    ->update();
    }

    public function deleteEntry($where) {
    return $this->db
    ->table($this->table)
    ->where($where)
    ->delete();
    }

    public function getEntry($where) {
    return $this->db
    ->table($this->table)
    ->where($where)
    ->get()
    ->getRow();

    }

    public function getEntryList($where = 0, $start = 0, $limit = 0, $columnName = 0, $orderBy = 0) {
    $builder = $this->db->table($this->table);
    if($where)
    $builder->where($where);
    if($columnName && $orderBy)
    $builder->orderBy($columnName, $orderBy);
    if($limit)
    $builder->limit($limit, $start);
    return $builder
    ->get()
    ->getResult();
    }

    }

     


    3. Create the Controller

    Create a controller named CustomerApi.php in the app/Controllers/api/v1 directory.

    class CustomerApi extends BaseController {

    public function __construct() {
    $db = db_connect();

    $this->customer = new CustomerModel($db);

    $this->ip_address = $_SERVER['REMOTE_ADDR'];
    $this->datetime = date("Y-m-d H:i:s");
    }


    public function list() {
    $start_limit = $this->request->getPost('start_limit');
    $load_limit = $this->request->getPost('load_limit');
    $type = $this->request->getPost('type');
    $where = [
    'type' => $type,
    ];
    $result = $this->customer->getEntryList($where, $start_limit, $load_limit, 'name', 'ASC');
    if($result) {
    $json = [
    'status' => true,
    'message' => 'Fetched successfully',
    'results' => $result,
    ];
    } else {
    $json = [
    'status' => false,
    'message' => 'Something went wrong. Please try again!',
    ];
    }
    echo json_encode($json);
    }

    public function create() {
    $name = $this->request->getPost('name');
    $mobile_country = $this->request->getPost('mobile_country');
    $mobile_number = $this->request->getPost('mobile_number');
    $address = $this->request->getPost('address');
    $status = $this->request->getPost('status');
    $type = $this->request->getPost('type');
    $where = [
    'mobile_country' => $mobile_country,
    'mobile_number' => $mobile_number,
    ];
    $mobile_result = $this->customer->getEntry($where);
    if($mobile_result) {
    $json = [
    'message' => "Entered mobile number is already exists.",
    'status' => false,
    ];
    } else {
    $data = [
    'name' => $name,
    'mobile_country' => $mobile_country,
    'mobile_number' => $mobile_number,
    'address' => $address,
    'type' => $type,
    'ip_address' => $this->ip_address,
    'created_at' => $this->datetime,
    'status' => $status,
    ];
    $result = $this->customer->addEntry($data);
    if($result) {
    $json = [
    'message' => "Customer has been created successfully.",
    'status' => true,
    ];
    } else {
    $json = [
    'message' => "Something went wrong. Please try again.",
    'status' => false,
    ];
    }
    }
    echo json_encode($json);
    }

    public function details() {
    $customer_id = $this->request->getPost('customer_id');
    $where = [
    'id' => $customer_id,
    ];
    $result = $this->customer->getEntry($where);
    if($result) {
    $json = [
    'status' => true,
    'message' => 'Fetched successfully',
    'result' => $result,
    ];
    } else {
    $json = [
    'status' => false,
    'message' => 'Something went wrong. Please try again!',
    ];
    }
    echo json_encode($json);
    }

    public function update() {
    $customer_id = $this->request->getPost('customer_id');
    $name = $this->request->getPost('name');
    $mobile_country = $this->request->getPost('mobile_country');
    $mobile_number = $this->request->getPost('mobile_number');
    $address = $this->request->getPost('address');
    $status = $this->request->getPost('status');
    $type = $this->request->getPost('type');
    $where = [
    'id !=' => $customer_id,
    'mobile_country' => $mobile_country,
    'mobile_number' => $mobile_number,
    ];
    $mobile_result = $this->customer->getEntry($where);
    if($mobile_result) {
    $json = [
    'message' => "Entered mobile number is already exists.",
    'status' => false,
    ];
    } else {
    $data = [
    'name' => $name,
    'mobile_country' => $mobile_country,
    'mobile_number' => $mobile_number,
    'address' => $address,
    'type' => $type,
    'ip_address' => $this->ip_address,
    'status' => $status,
    ];
    $where = [
    'id' => $customer_id,
    ];
    $result = $this->customer->updateEntry($where, $data);
    if($result) {
    $json = [
    'message' => "Customer has been updated successfully.",
    'status' => true,
    ];
    } else {
    $json = [
    'message' => "Something went wrong. Please try again.",
    'status' => false,
    ];
    }
    }
    echo json_encode($json);
    }

    public function delete() {
    $customer_id = $this->request->getPost('customer_id');
    $where = [
    'id' => $customer_id,
    ];
    $result = $this->customer->deleteEntry($where);
    if($result) {
    $json = [
    'status' => true,
    'message' => 'Selected customer has been deleted successfully.',
    ];
    } else {
    $json = [
    'status' => false,
    'message' => 'Something went wrong. Please try again!',
    ];
    }
    echo json_encode($json);
    }
    }

     


    4. Configure Routes

    Add the following routes to your app/Config/Routes.php file to set up the API endpoints.

    $routes->group('api', function($routes) {
    $routes->group('v1', function($routes) {
    // Customers
    $routes->group('customer', function($routes) {
    $routes->post('list', 'api/v1/CustomerApi::list');
    $routes->post('create', 'api/v1/CustomerApi::create');
    $routes->post('details', 'api/v1/CustomerApi::details');
    $routes->post('update', 'api/v1/CustomerApi::update');
    $routes->post('delete', 'api/v1/CustomerApi::delete');
    });
    });
    });

     

    NOTE: Please replace the forward slash in Routes.php with the backward slash in your code.

    This setup provides a complete RESTful API for managing customers in your Expense Manager App using CodeIgniter 4.