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 Payment modes 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 Product API

    1. Database Setup

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

    CREATE TABLE `product_info` (
      `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
      `name` varchar(100) NOT 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 PaymentTypeModel.php in the app/Models/payment_type directory.


    class PaymentTypeModel extends Model {

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

    $this->table = 'payment_type_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 PaymentTypeApi.php in the app/Controllers/api/v1 directory.

    public function create() {
    $name = $this->request->getPost('name');

    $payment_result = $this->paymentType->getEntry(['name' => $name]);
    if($payment_result) {
    $json = [
    'message' => "Entered payment type is already exists.",
    'status' => false,
    ];
    } else {
    $data = [
    'name' => $name,
    'ip_address' => $this->ip_address,
    'created_at' => $this->datetime,
    'status' => '1',
    ];
    $result = $this->paymentType->addEntry($data);
    if($result) {
    $json = [
    'message' => "Payment type has been created successfully.",
    'status' => true,
    ];
    } else {
    $json = [
    'message' => "Something went wrong. Please try again.",
    'status' => false,
    ];
    }
    }
    echo json_encode($json);
    }

    public function details() {
    $payment_type_id = $this->request->getPost('payment_type_id');
    $result = $this->paymentType->getEntry(['id' => $payment_type_id]);
    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() {
    $payment_type_id = $this->request->getPost('payment_type_id');
    $name = $this->request->getPost('name');

    $product_result = $this->paymentType->getEntry(['name' => $name, 'id !=' => $payment_type_id]);
    if($product_result) {
    $json = [
    'message' => "Entered payment type is already exists.",
    'status' => false,
    ];
    } else {
    $data = [
    'name' => $name,
    'ip_address' => $this->ip_address,
    ];
    $result = $this->paymentType->updateEntry(['id' => $product_id], $data);
    if($result) {
    $json = [
    'message' => "Payment type has been updated successfully.",
    'status' => true,
    ];
    } else {
    $json = [
    'message' => "Something went wrong. Please try again.",
    'status' => false,
    ];
    }
    }
    echo json_encode($json);
    }

    public function delete() {
    $payment_type_id = $this->request->getPost('payment_type_id');
    $where = [
    'id' => $payment_type_id,
    ];
    $result = $this->paymentType->deleteEntry($where);
    if($result) {
    $json = [
    'status' => true,
    'message' => 'Selected payment type 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) {
    // Payment Types / Modes
    $routes->group('payment-type', function($routes) {
    $routes->post('list', 'api/v1/PaymentTypeApi::list');
    $routes->post('create', 'api/v1/PaymentTypeApi::create');
    $routes->post('details', 'api/v1/PaymentTypeApi::details');
    $routes->post('update', 'api/v1/PaymentTypeApi::update');
    $routes->post('delete', 'api/v1/PaymentTypeApi::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.