Codeigniter Restful API Tutorial Example

By Hardik Savani November 5, 2023 Category : PHP Codeigniter

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...

Shares