Working with RESTful services in Codeigniter – DELETE example

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

You may also be interested in Working with RESTful services in Codeigniter – GET example, Working with RESTful services in Codeigniter – POST example  and Working with RESTful services in Codeigniter – PUT example

Introduction

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

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 https://philsturgeon.uk/codeigniter/2009/06/03/REST-implementation-for-CodeIgniter/

Prerequisites

Please go through Setup RESTful service with Codeigniter

Example

Step 1. Create a MySQL table contact under database roytuts

Step 2. Dump some data into table contact

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

modify also <root directory>/application/config/database.php

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

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: DELETE

URL: http://localhost/ci_3_1_0/index.php/restdeletecontroller/delete_contact/13

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

Output:

Step 9. Check the database, you will see that row with contact_id=13 has been deleted successfully.

Thanks for reading.

Soumitra

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.