Restful API web services with CodeIgniter

What is REST ?

REST stands for Representational State Transfer . In very simple terms, In REST we have basically two broader actions Request and Response. Client will send the request to the REST API in a data format (i.e, JSON, XML ) that is acceptable by the API. Similarly, a Response will be generated by the API in a pre-defined(i.e, JSON, XML ) format.
According to the REST guidelines, we have to make explicit use of the HTTP methods to make request to a REST based API. So the requests for basic CRUD operation in any application can be mapped to the HTTP methods or verbs in this way:

  • POST method to Create or Add new data ressource.
  • PUT method to Update an existing data ressource.
  • GET method to retrieve an existing data ressource.
  • DELETE method to remove or delete an existing data ressource.

We can use CodeIgniter to create a RESTful API for your existing web applications, and demonstrate how to interact with your own API or other RESTful web-services, such as Facebook and Twitter.

url-partitions

 

RESTful Concept

Before diving into the implementation, let’s first review the RESTful concept. Representational State Transfer (REST) is an architectural model for web services rather than a standard. As REST has grown to be the dominant model for web services over the past few years, however, most implementations follow common design patterns.

Use Nouns instead of Verbs

Your RESTful API should provide access to things and not actions.

So, instead of this:

createCustomer
getCustomer?id=666

Do this:

POST /customers
GET /customers/666

Everything Has an ID

The beauty of REST is its simplicity. In a sense, REST is really just a collection of Universal Resource Identifiers (URIs). To be an identifier, every resource should have a unique ID.

GET /customers/666
GET /products/4234

In this video we will see how to install CodeIgniter under linux and set up a simple hello rest web service in few minutes.

[flv:video/rest-server-codeigniter.flv 480 360]

Part 1 – Creating a RESTful API

Step 1: Setting up the Demo

Firstly you need to download the codeigniter-restserver code from GitHub and extract it and move the code to your server. A fully RESTful server implementation for CodeIgniter using one library (codeigniter-restserver), one config file and one controller.

Step 2: The URLs

With the files extracted and the base_url set, we are ready to load up our RESTful CodeIgniter installation, and have a look at the demo supplied with it. Browse the base URL, which by default is:

http://localhost/restserver

Step 3: The Code

Now if you open up application/controllers/example_api.php you will immediatley spot a few differences from normal CodeIgniter controllers.

REST_Controller

In the MVC pattern, a controller is the central point of the logic. It is called when a user makes a request and then based on the logic in the controller it fetches data and outputs views. CodeIgniter contains its own logic for how a Controller should work, but as we are doing something different we need our own REST_Controller library to contain its own REST related logic. So simply use:

	<?php
	require(APPPATH'.libraries/REST_Controller.php');
	
	class Example_api extends REST_Controller {
	
	}

extradrmtech

Since 30 years I work on Database Architecture and data migration protocols. I am also a consultant in Web content management solutions and medias protecting solutions. I am experienced web-developer with over 10 years developing PHP/MySQL, C#, VB.Net applications ranging from simple web sites to extensive web-based business applications. Besides my work, I like to work freelance only on some wordpress projects because it is relaxing and delightful CMS for me. When not working, I like to dance salsa and swing and to have fun with my little family.

You may also like...