HDTuto.com

Codeigniter REST API Tutorial Example



Now, let's see post of restful web services using codeigniter. if you have question about codeigniter 3 rest api tutorial then i will give simple example with solution. you will learn codeigniter 3 rest api with get. step by step explain post. Follow bellow tutorial step of put.

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




May 24, 2020 | Category : Codeigniter



Blog Search