In this tutorial I am going to show you how we can work with RESTful or REST webservice in Codeigniter framework.

You may also like the Working with RESTful services in Codeigniter – POST example,  Codeigniter GET example with REST service and Working with RESTful services in Codeigniter – DELETE example


The most important concept in REST is resources, which are identified by global IDs — typically using URIs. Client applications use HTTP methods (GET/ POST/ PUT/ DELETE) to manipulate the resource or collection of resources. A RESTful Web service is implemented using HTTP and the principles of REST. Typically, a RESTful Web service should define the following aspects:

The base/root URI for the Web service such as http://<host>/<appcontext/contextpath>/<url pattern>/<resources>.
The MIME type of the response data supported, which are JSON/XML/TEXT/HTML etc.
The set of operations supported by the service. (for example, POST, GET, PUT or DELETE).


HTTP methods are mapped to CRUD (create, read, update and delete) actions for a resource. Although you can make slight modifications such as making the PUT method to create or update, the basic patterns are listed as follows.

HTTP GET: Get/List/Retrieve an individual resource or a collection of resources.
HTTP POST: Create a new resource or resources.
HTTP PUT: Update an existing resource or collection of resources.
HTTP DELETE: Delete a resource or collection of resources.

You may read the section on REST API for Codeigniter


Please go through Setup RESTful service with Codeigniter


Step 1. Create a MySQL table contact under database roytuts

modify <root directory>/application/config/autoload.php file for auto-loading html, url, file, form and database

Step 2. Dump some data into table contact

Step 3. Modify also <root directory>/application/config/database.php

Step 4. Create a controller file RestPutController.php under <project root>/application/controllers with the following source code

  • reads in PUT arguments.

Step 5. Create a model file ContactModel.php under <project root>/application/models with the following source code

Step 6. Modify file <project root>/application/config/routes.php file

Step 7. Now run the application on apache http server.

Step 8. Use the REST Client in Firefox browser to test the REST service.

Method: PUT

Headers: Add Custom Header as name = Content-Type, value=application/json

URL: http://localhost/ci_3_1_0/index.php/restputcontroller/update_contact

Body: {“contact_id”:”12″, “contact_name”:”S Roy”, “contact_address”:””, “contact_phone”:”1234567890″}

$[‘rest_default_format’]=’json’ at <root directory>/application/config/rest.php


Step 9. Check the database table, you will see the row has been updated with below data

Thanks for reading.

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.