Codeigniter Restful API Tutorial Example
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
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
<?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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Codeigniter Get Last Executed Query Log Example
- Codeigniter Dynamic Highcharts Example
- Codeigniter Create Zip File and Download Example
- Codeigniter Google Recaptcha Form Validation Example
- Codeigniter Ajax Pagination using JQuery Example
- Codeigniter JQuery Ajax Autocomplete Search using Typeahead
- Codeigniter Confirm Box Before Delete Record Example
- How to implement and use DataTables in CodeIgniter?
- Codeigniter 3 - Basic CRUD application with MySQL Example with Demo
- Codeigniter Drag and Drop Multiple Image Upload Example
- Codeigniter Dynamic Dependent Dropdown using Ajax Example
- Codeigniter Generate PDF from View using Dompdf Example
- Codeigniter Ajax CRUD Tutorial Example
- Codeigniter Select2 Ajax Autocomplete from Database Example