How to create Rest API in codeigniter?

Today, We want to share with you rest api in codeigniter.In this post we will show you codeigniter api tutorial, hear for codeigniter api example we will give you demo and example for implement.In this post, we will learn about REST API CRUD Example in PHP with MySQLi with an example.

Working with RESTful Services in CodeIgniter

How to Create a REST API in Codeigniter with Basic Authentication with Example.

Step 1: Create products Table

Example 1:

CREATE TABLE IF NOT EXISTS `products` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `description` varchar(255) NOT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=

Step 2: Create rest.php config file

application/config/rest.php
download file from here: Download Zip file :

Read Also:  Laravel 6 Ajax post data Example

Step 3: Create libraries files

application/libraries/REST_Controller.php
Download Zip file

application/libraries/REST_Controller.php

download file from here: Download Zip file

application/libraries/Format.php

download file from here: Download Zip file

Download Zip file

Step 4: Create API Controller

application/controllers/api/Product.php

<?php
   
require APPPATH . 'libraries/REST_Controller.php';
     
class Product extends REST_Controller {
    
	  /**
     * Get All Data from this method.
     *
     * @return Response
    */
    public function __construct() {
       parent::__construct();
       $this->load->database();
    }
       
    /**
     * Get All Data from this method.
     *
     * @return Response
    */
	public function index_get($id = 0)
	{
        if(!empty($id)){
            $data = $this->db->get_where("products", ['id' => $id])->row_array();
        }else{
            $data = $this->db->get("products")->result();
        }
     
        $this->response($data, REST_Controller::HTTP_OK);
	}
      
    /**
     * Get All Data from this method.
     *
     * @return Response
    */
    public function index_post()
    {
        $input = $this->input->post();
        $this->db->insert('products',$input);
     
        $this->response(['Product created successfully.'], REST_Controller::HTTP_OK);
    } 
     
    /**
     * Get All Data from this method.
     *
     * @return Response
    */
    public function index_put($id)
    {
        $input = $this->put();
        $this->db->update('products', $input, array('id'=>$id));
     
        $this->response(['Product updated successfully.'], REST_Controller::HTTP_OK);
    }
     
    /**
     * Get All Data from this method.
     *
     * @return Response
    */
    public function index_delete($id)
    {
        $this->db->delete('products', array('id'=>$id));
       
        $this->response(['Product deleted successfully.'], REST_Controller::HTTP_OK);
    }
    	
}

Now simply you can run above listed some url like as bellow URL:

  • Product List API:
  • Product Create API:
  • Product Show API:
  • Product Update API:
  • Product Delete API:
Read Also:  vue Image Upload Component

Run URL

//Product List API
http://localhost:8080/api/product

//Product Create API
http://localhost:8080/api/product

//Product Show API:
http://localhost:8080/api/product/5

//Product Update API
http://localhost:8080/api/product/15

//Product Delete API
http://localhost:8080/api/product/11

I hope you get an idea about Create and Test REST API in CodeIgniter.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.