Working with RESTful services in Codeigniter – GET example

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

You may like also Working with RESTful services in Codeigniter – POST example, Working with RESTful services in Codeigniter – PUT example and Working with RESTful services in Codeigniter – DELETE 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 RestGetController.php under <project root>/application/controllers with the following source code

  • is used to return GET variables from either a query string like this index.php/restgetcontroller/contact?id=1 or can be set in the more CodeIgniter’s way with index.php/restgetcontroller/contact/id/1.

In the method, we are picking up a ?id=XX and passing it to the model. If data is found we send it to the $this->response() function with a status 200. If nothing is found, return no body and a 404 to say nothing was found.

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

URL: http://localhost/ci_3_1_0/index.php/restgetcontroller/contacts

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

Output:

If you change as, $[‘rest_default_format’]=’xml’ at <root directory>/application/config/rest.php

Method: GET

URL: http://localhost/ci_3_1_0/index.php/restgetcontroller/contact?id=1

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

Output:

If you change as, $[‘rest_default_format’]=’xml’ at <root directory>/application/config/rest.php

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.