REST using Codeigniter

  • codeigniter

One of the most common demands among clients today is the integration of REST-based web services in their web application. In fact, this demand seems quite growing, as the demand for the mobile application is increasing. Since Mobile applications are device or OS specific, the REST architecture comes to our rescue under such conditions, for the server side implementations.

What is REST ?

REST stands for Representational State Transfer. For a more in depth understanding visit: IBM Library or Wikipedia. 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. For the sake of simplicity, our response and request format will be in JSON. You can easily modify it to suit your particular requirements.

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 in this way:

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

Building the REST ?

We are going to use the above guidelines in order to build a very basic type of REST server. Let us name this file as Rest_service.php, and we will place it in /application/libraries folder.

		
			class Rest_server extends CI_Controller {
		    /*
		     * Holds the type of request made by the server
		     * 
		     * @var     string
		     * @access  protected
		     * @since   0.0.1 
		     */
		    protected $_request_type;
		    
		    //----------------------------------------------------------
		    
		    /**
		     * Constructor Method
		     * 
		     * @since       0.0.1
		     * @version     0.0.1
		     */
		    public function __construct() {
		        parent::__construct();
		        $this->_request_type = $this->_check_request_method();
		        $this->_call_requested_function();
		    }
		    
		    //----------------------------------------------------------
		    
		    /*
		     * Checks which method has been used to make the request.
		     * 
		     * @version     0.0.1
		     * @since       0.0.1
		     */
		    function _check_request_method() {
		        $method = strtolower($this->input->server('REQUEST_METHOD'));
		        if (in_array($method, array('get', 'delete', 'post', 'put'))) {
		            return $method;
		        }
		        return 'get';
		    }
		    
		    //----------------------------------------------------------
		 
		    /**
		     * 
		     */
		    function _call_requested_function(){
		        #processing the arguments based on the type of request
		        switch ($this->_request_type) {
		            case 'get':
		                #converting the passed arguments into an associative array
		                $data = $this->uri->uri_to_assoc(4);
		                break;
		            case 'post':
		                $data = file_get_contents("php://input");
		                $data = json_decode($data);
		                break;
		            case 'put':
		                $data = file_get_contents("php://input");
		                $data = json_decode($data);
		                break;
		            case 'delete':
		                $data = file_get_contents("php://input");
		                $data = json_decode($data);
		            default:
		                break;
		        }
		        
		        #inserting the data entered by the user in the POST array
		        if (!empty($data)) {
		            foreach ($data as $key => $value) {
		                $_POST[$key] = $value;
		            }
		        }
		        
		        
		        #reading the object name for which the call is made
		        $object_name = $this->uri->segment(3);
		        $controller_method = $object_name . '_' . $this->_request_type;

		        #calling the target function
		        call_user_func(array($this, $controller_method), $arr_argument='');
		    }    
		    
		}
		
	

This class will handle all the requests send to the Rest service and will call the appropriate methods to perform the requested operation.

To view or download the entire class visit this link