Codeigniter 3: Create Restful API with example

Codeigniter 3: Create Restful API with example

In this post we will give you information about Codeigniter 3: Create Restful API with example. Hear we will give you detail about Codeigniter 3: Create Restful API with exampleAnd how to use it also give you demo for it if it is necessary.

In this Codeigniter 3 Tutorial, I will let you know about RESTful web services.

RESTful we services are a medium to exchange data between applications or system on the internet.

RESTful web services are used for creating APIs because they are lightweight and loosely coupled web services. With the help of APIs, you can create a bridge between backend system and modern mobile application.

RESTful web service uses HTTP methods like get, post, put and delete.

Today we’ll know how we can create a RESTful API in Codeigniter in order to exchange data between different applications.

As we know, Codeigniter is a PHP based web application framework and for the beginners it’s very easy and simple to learn.

In this example, I will create a rest apis for “users” module in Codeigniter 3 application.


Step 1: Create products Table

In order to create restful web services, we need to create a table first in MySQL database.


CREATE TABLE 'products' (
 'id' int(10) unsigned NOT NULL AUTO_INCREMENT,
 'name' varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 'price' double NOT NULL,
 'created_at' timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
 'updated_at' datetime DEFAULT NULL,
 PRIMARY KEY ('id')
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci



Step 2: Configure REST Controller Library

In this step, we will configure the REST Controller Library in our Codeigniter application.

Below are the few steps to setup REST Controller :

  1. Download the Rest config file and place that file in the application/config/ directory.
  2. Download the REST_Controller.php and Format.php file and place that files in the application/libraries/ directory.
  3. Download the Language file and place that file in the application/language/english/ directory.

Update the application/config/config.php from $config[‘subclass_prefix’] = ‘MY_’; to : $config[‘subclass_prefix’] = ‘REST_’;


Step 3: Create API Controller

In this step, I will create a api folder in the controllers diectory and inside the api folder create a file “Product.php” and put the below code into that file.


application/controllers/api/Product.php

<?php
   
   require APPPATH .'/libraries/REST_Controller.php';
   use RestserverLibrariesREST_Controller;
     
classProductextends REST_Controller {
    
	  /**
     * Get All Data from this method.
     *
     * @return Response
    */
    publicfunction__construct() {
       parent::__construct();
       $this->load->database();
    }
       
    /**
     * Get All Data from this method.
     *
     * @return Response
    */
	publicfunctionindex_get($id=)
	{
        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
    */
    publicfunctionindex_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
    */
    publicfunctionindex_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
    */
    publicfunctionindex_delete($id)
    {
        $this->db->delete('products', array('id'=>$id));
       
        $this->response(['Product deleted successfully.'], REST_Controller::HTTP_OK);
    }
    	
}

You can test the apis through Postman Extension :

Product List API :

Add New Product API :

View Product Details API :

Update Product Details API :

Delete Product Data API :

Create restful api in Laravel 5 with resourceful routes

Label :

PHP

How To

API

MVC

Web Development

Codeigniter

Hope this code and post will helped you for implement Codeigniter 3: Create Restful API with example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

5  +  5  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US