{"id":1745,"date":"2014-01-25T10:44:41","date_gmt":"2014-01-25T08:44:41","guid":{"rendered":"http:\/\/www.extradrm.com\/?p=1745"},"modified":"2020-09-07T19:54:00","modified_gmt":"2020-09-07T17:54:00","slug":"build-an-restful-api-with-codeigniter","status":"publish","type":"post","link":"https:\/\/www.extradrm.com\/?p=1745","title":{"rendered":"Restful API web services with CodeIgniter"},"content":{"rendered":"<h3>What is REST ?<\/h3>\n<p>REST stands for\u00a0<i>Representational State Transfer<\/i> . In very simple terms, In REST we have basically two broader actions\u00a0<b><i>Request<\/i><\/b>\u00a0and\u00a0<b><i>Response<\/i><\/b>. Client will send the request to the REST API in a data format (i.e,\u00a0<i>JSON, XML\u00a0<\/i>) that is acceptable by the API. Similarly, a Response will be generated by the API in a pre-defined(i.e,\u00a0<i>JSON, XML\u00a0<\/i>) format.<br \/>\nAccording 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 <strong>HTTP methods or verbs<\/strong> in this way:<\/p>\n<ul>\n<li><b>POST\u00a0<\/b>method to Create or Add new data ressource.<\/li>\n<li><b>PUT\u00a0<\/b>method to Update an existing data ressource.<\/li>\n<li><b>GET<\/b>\u00a0method to retrieve an existing data ressource.<\/li>\n<li><b>DELETE\u00a0<\/b>method to remove or delete an existing data ressource.<\/li>\n<\/ul>\n<p>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.<\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/url-partitions.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1746\" alt=\"url-partitions\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/url-partitions.png\" width=\"600\" height=\"119\" srcset=\"https:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/url-partitions.png 600w, https:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/url-partitions-300x59.png 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h3>RESTful Concept<\/h3>\n<p>Before diving into the implementation, let\u2019s first review the RESTful concept. <a title=\"Representational State Transfer\" href=\"http:\/\/en.wikipedia.org\/wiki\/Representational_state_transfer\" target=\"_blank\" rel=\"noopener noreferrer\">Representational State Transfer<\/a> (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.<\/p>\n<h4>Use Nouns instead of Verbs<\/h4>\n<p>Your RESTful API should provide access to things and not actions.<\/p>\n<p>So, instead of this:<\/p>\n<pre>createCustomer\r\ngetCustomer?id=666<\/pre>\n<p>Do this:<\/p>\n<pre>POST \/customers\r\nGET \/customers\/666<\/pre>\n<h4>Everything Has an ID<\/h4>\n<p>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.<\/p>\n<pre>GET \/customers\/666\r\nGET \/products\/4234<\/pre>\n<p>In this video we will see how to install CodeIgniter under linux and set up a simple hello rest web service in few minutes.<\/p>\n<p>[flv:video\/rest-server-codeigniter.flv 480 360]<\/p>\n<h3>Part 1 \u2013 Creating a RESTful API<\/h3>\n<h3>Step 1: Setting up the Demo<\/h3>\n<p>Firstly you need to download the <a href=\"http:\/\/github.com\/philsturgeon\/codeigniter-restserver\">codeigniter-restserver<\/a> code from GitHub and extract it and move the code to your server. A fully RESTful server implementation for CodeIgniter using one library (<a href=\"http:\/\/github.com\/philsturgeon\/codeigniter-restserver\">codeigniter-restserver<\/a>), one config file and one controller.<\/p>\n<h3>Step 2: The URLs<\/h3>\n<p>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:<\/p>\n<pre>http:\/\/localhost\/restserver<\/pre>\n<h3>Step 3: The Code<\/h3>\n<p>Now if you open up <em>application\/controllers\/example_api.php<\/em> you will immediatley spot a few differences from normal CodeIgniter controllers.<\/p>\n<h4>REST_Controller<\/h4>\n<p>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:<\/p>\n<pre>\r\n\t&lt;?php\r\n\trequire(APPPATH'.libraries\/REST_Controller.php');\r\n\t\r\n\tclass\u00a0Example_api\u00a0extends\u00a0REST_Controller\u00a0{\r\n\t\r\n\t}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>What is REST ? REST stands for\u00a0Representational State Transfer . In very simple terms, In REST we have basically two broader actions\u00a0Request\u00a0and\u00a0Response. Client will send the request to the REST API in a data&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":2841,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,267,5,10,477],"tags":[],"youtube_video":null,"_links":{"self":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/1745"}],"collection":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1745"}],"version-history":[{"count":0,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/1745\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/media\/2841"}],"wp:attachment":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}