Codeigniter 3 Restful API Tutorial

Codeigniter 3 Restful API Tutorial

In this post we will give you information about Codeigniter 3 Restful API Tutorial. Hear we will give you detail about Codeigniter 3 Restful API TutorialAnd how to use it also give you demo for it if it is necessary.

In this tutorial, i would like to share with you step by step tutorial of creating restful web services in codeigniter 3 project. we will create rest api which uses HTTP method likes GET, PUT, POST, DELETE.

you can learn how to make setup for your rest api in codeigniter. we will create rest web services using codeigniter restserver.

In Today, As we know codeigniter is a php framework. So many of the developer choose codeigniter to create rest api for mobile app developing. Yes Web services is a very important when you create web and mobile developing, because you can create same database and work with same data.

we will create rest api for “items” module and we will create api for listing, creating, showing, editing and deleting methods. so let’s follow bellow step to create restful api.

Step 1: Create items Table

In first table we must have one table with some dummy records. For this example i created “items” table, so run bellow query:

CREATE TABLE IF NOT EXISTS 'items' (

'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

In this step we need to add one config file for rest api configuration. so create file and add code like as bellow:

application/config/rest.php

download file from here: Download Zip file

Also see:Codeigniter 3 – select2 ajax autocomplete from database example with demo

Step 3: Create libraries files

In this step, we will create library files. we will create two file one is REST_Controller.php and Format.php file in libraries folder.

application/libraries/REST_Controller.php

download file from here: Download Zip file

application/libraries/Format.php

download file from here: Download Zip file

Step 4: Create API Controller

In this step, we will write code of controller file. so first create “api” folder in controllers directory. than create “Item.php” controller and copy bellow code.

application/controllers/api/Item.php

Also see:Codeigniter Ajax Form Submit Example

<?php

require APPPATH . 'libraries/REST_Controller.php';

class Item 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("items", ['id' => $id])->row_array();

}else{

$data = $this->db->get("items")->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('items',$input);

$this->response(['Item 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('items', $input, array('id'=>$id));

$this->response(['Item updated successfully.'], REST_Controller::HTTP_OK);

}

/**

* Get All Data from this method.

*

* @return Response

*/

public function index_delete($id)

{

$this->db->delete('items', array('id'=>$id));

$this->response(['Item deleted successfully.'], REST_Controller::HTTP_OK);

}

}

Now simply you can run above listed url like as bellow screen shot:

Item List API:

Item Create API:

Imte Show API:

Item Update API:

Item Delete API:

I hope it can help you…

Hope this code and post will helped you for implement Codeigniter 3 Restful API Tutorial. 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 *

4  +  5  =  

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