SlideShare a Scribd company logo
REST API & 
Implementing it in 
CodeIgniter
Who Am I 
• PHP Developer & Consultant 
• Reviewed “Testing with Qunit” 
• Helped to bring out thesis on “Business 
Prospective of cloud computing” 
• Founder of Website “WebGunny.com” 
RIP Jun 2010 - Dec 2011
In this talk... 
• Why REST API is a heart of every product 
• REST API – As developer UI 
• Best Practices of REST API 
• REST API in codeigniter
Single Source of Truth
Your App is not isolated
Developer is your API customer
Building Developer Friendly API
Let’s Start with best practices 
API End Point : 
https://www.YourApp.com/Api/ 
OR 
https://Api.YourApp.com/ 
Finally Format: 
https://www.YourApp.com/Api/ResourceName
Identifying resources 
• You can make the resources more sensible 
based on your product 
• For example 
– Tasks 
– Comments 
– Notifications 
– Users 
– Projects 
– Files
JSON Everywhere
Make Use of HTTP Verbs 
• GET /task - Retrieves a list of task 
• GET /task/12 - Retrieves a specific task 
• POST /task - Creates a new task 
• PUT /task/12 - Updates task #12 
• PATCH /task/12 - Partially updates task #12 
• DELETE /task/ - Deletes all task 
• DELETE /task/12 - Deletes task #12
Map the relationships 
• GET /task/12/comments - Retrieves list of comments 
for task #12 
• GET /task /12/comments/5 - Retrieves comment #5 for 
task #12 
• POST /task /12/comments - Creates a new comments 
in task #12 
• PUT /task /12/comments/5 - Updates comments #5 for 
task #12 
• PATCH /task /12/comments/5 - Partially updates 
comment #5 for task #12 
• DELETE /task/12/comments/5 - Deletes comment #5 
for task #12
Search Sort & Filter 
• GET /tasks?sort=-priority - Retrieves a list of 
task in descending order of priority 
• GET /tasks?sort=-priority,created_at - 
Retrieves a list of tasks in descending order of 
priority then by date created
Aliases for common queries 
To make the API experience more pleasant for 
the average consumer 
GET /tasks?status=completed 
GET /tasks/recently_completed
Allow the fields to be selected 
The API consumer doesn't always need the full 
representation of a resource. 
GET /task?fields=id,title,updated_at
Paging of data 
Paging makes the API fast & responsive 
GET /notification?page=1&per_page=50
Return full resource after action 
• A PUT, POST or PATCH call may make 
modifications to fields 
• Return the updated (or created) 
representation as part of the response. 
• Prevent an API consumer from having to hit 
the API again
Auto loading related 
resources 
{ "id" : 12, 
“TaskName" : "I have a question!", 
"summary" : "Hi, ....", 
"customer" : { "name" : "Bob" }, 
assigned_user: { "id" : 42, "name" : "Jim", } 
}
Make Error Message Friendly 
• The API should always return sensible HTTP 
status codes 
• 400 series status codes for client issues & 500 
series status codes for server issues 
• API should standardize that all 400 series errors 
come with consumable JSON error 
representation 
{ "code" : 1234, 
"message" : “task field validation failed ", 
"description" : “Due date is not set" 
}
Authentication
API Status History
Documentation
REST API in Codeigniter 
UI/ Controller 
Your App 
Your API 
Rest Client
What we need 
• Codeigniter 
• chriskacerguis/codeigniter-restserver 
• Router implementation
Structuring the project 
/application 
/controller/ 
api/ //For all api controllers 
/libraries //For the third-party libraries 
REST_server.php 
Format.php 
/config //For all config files 
Router.php 
Rest_server.php
Router Implementation 
//res/id/function/id --> res/function/id/num/sid/num 
$route['api/([a-z_]+)/(:any)/([a-z_]+)/(:any)'] = 'api/$1/$3/id/$2/rid/$4'; 
//res/id/function --> res/function/id/num 
$route['api/([a-z_]+)/(:any)/([a-z_]+)'] = 'api/$1/$3/id/$2'; 
//res/function --> res/function 
$route['api/([a-z_]+)/([a-z_]+)'] = 'api/$1/$2/'; 
//res/id --> res/index/id/num 
$route['api/([a-z_]+)/(:any)'] = 'api/$1/index/id/$2'; 
//res/ --> //res/ 
$route['api/([a-z_]+)'] = 'api/$1';
Creating a first API controller 
require(APPPATH . '/libraries/Rest_Service.php'); 
class task extends REST_Service{ 
public function index_get() { //Logic } 
public function index_post() { //Logic } 
public function index_put() { //Logic } 
public function index_patch() { //Logic } 
public function index_delete() { //Logic } 
}
Every Function has 2 Reaction 
public function index_get() { 
if($this->get('id')) 
{ 
//Application Logic 
$this->response($results,$code); 
} 
else 
{ 
//Application Logic 
$this->response($results,$code); 
} 
}
HTTP Action Vs SQL 
• Get (select) 
– Get All 
– Get by ID 
• Post (Insert) 
• Put (update all fields ) 
• Patch (update selected fields) 
• Delete (delete) 
– Delete All 
– Delete by ID
Summary 
• REST API is heart of product 
• REST API is a developer UI 
• Follow the best practices of REST API 
• Use “chriskacerguis/codeigniter-restserver” to 
implement REST in codeigniter
Questions ? 
Website:SachinGKulkarni.com 
Twitter:@sachingk30 
Email:sachingk.30@gmail.com

More Related Content

PDF
RESTful API Design & Implementation with CodeIgniter PHP Framework
PPTX
Apache Knox - Hadoop Security Swiss Army Knife
PPTX
NGINX, Istio, and the Move to Microservices and Service Mesh
PPTX
Next.js - ReactPlayIO.pptx
PPTX
Api Testing
ODP
Boost your App with Gatling
PDF
Api Testing.pdf
PPTX
RESTful API Testing using Postman, Newman, and Jenkins
RESTful API Design & Implementation with CodeIgniter PHP Framework
Apache Knox - Hadoop Security Swiss Army Knife
NGINX, Istio, and the Move to Microservices and Service Mesh
Next.js - ReactPlayIO.pptx
Api Testing
Boost your App with Gatling
Api Testing.pdf
RESTful API Testing using Postman, Newman, and Jenkins

What's hot (20)

PPTX
Gatling overview
PPTX
Learning Svelte
PDF
CKA Certified Kubernetes Administrator Notes
PDF
Three Lessons about Gatling and Microservices
PPT
Postman.ppt
PDF
Celery - A Distributed Task Queue
PPTX
Apache Lucene Basics
PPT
API 101 - Understanding APIs
PPTX
RESTful API - Best Practices
PPTX
Presentation on Apache Jmeter
PDF
Svelte as a Reactive Web Framework
PPS
Scalable Web Architectures: Common Patterns and Approaches - Web 2.0 Expo NYC
PDF
LoadRunner Performance Testing
PDF
Services in kubernetes-KnolX .pdf
PDF
Quarkus tips, tricks, and techniques
PDF
Laravel Introduction
PDF
Jmeter Performance Testing
PDF
Introduction to E2E in Cypress
PPTX
Integration Success with AWS and Boomi
Gatling overview
Learning Svelte
CKA Certified Kubernetes Administrator Notes
Three Lessons about Gatling and Microservices
Postman.ppt
Celery - A Distributed Task Queue
Apache Lucene Basics
API 101 - Understanding APIs
RESTful API - Best Practices
Presentation on Apache Jmeter
Svelte as a Reactive Web Framework
Scalable Web Architectures: Common Patterns and Approaches - Web 2.0 Expo NYC
LoadRunner Performance Testing
Services in kubernetes-KnolX .pdf
Quarkus tips, tricks, and techniques
Laravel Introduction
Jmeter Performance Testing
Introduction to E2E in Cypress
Integration Success with AWS and Boomi
Ad

Viewers also liked (20)

PDF
Web Services PHP Tutorial
PDF
Consuming RESTful services in PHP
KEY
CodeIgniter 3.0
PDF
Advanced Web Services Hacking (AusCERT 06)
PDF
ACL in CodeIgniter
PDF
PHP and Web Services
PPTX
Secure Your REST API (The Right Way)
PPT
Develop webservice in PHP
PDF
Creating And Consuming Web Services In Php 5
PDF
REST API Doc Best Practices
PDF
You must know about CodeIgniter Popular Library
PDF
Servicio y Consumo de Servicios REST en PHP
ODP
CodeIgniter PHP MVC Framework
PDF
Criando e consumindo webservice REST com PHP e JSON
PPTX
REST & RESTful Web Services
PDF
RESTful Web Services
PPTX
Design Beautiful REST + JSON APIs
PPSX
CodeIgniter L3 model & active record & template
PDF
Api details for american syscorp
PPTX
Modular PHP Development using CodeIgniter Bonfire
Web Services PHP Tutorial
Consuming RESTful services in PHP
CodeIgniter 3.0
Advanced Web Services Hacking (AusCERT 06)
ACL in CodeIgniter
PHP and Web Services
Secure Your REST API (The Right Way)
Develop webservice in PHP
Creating And Consuming Web Services In Php 5
REST API Doc Best Practices
You must know about CodeIgniter Popular Library
Servicio y Consumo de Servicios REST en PHP
CodeIgniter PHP MVC Framework
Criando e consumindo webservice REST com PHP e JSON
REST & RESTful Web Services
RESTful Web Services
Design Beautiful REST + JSON APIs
CodeIgniter L3 model & active record & template
Api details for american syscorp
Modular PHP Development using CodeIgniter Bonfire
Ad

Similar to REST API Best Practices & Implementing in Codeigniter (20)

PPTX
REST-API introduction for developers
PPTX
API Workshop: Deep dive into REST APIs
PDF
Practices and tools for building better APIs
PDF
Practices and tools for building better API (JFall 2013)
PDF
PDF
Cqrs api v2
PDF
High quality ap is with api platform
PDF
Introduction to CloudStack API
PPTX
Apic dc api deep dive
PDF
Rest with Spring
PPTX
Super simple introduction to REST-APIs (2nd version)
ODP
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
PDF
Building Beautiful REST APIs with ASP.NET Core
PDF
Crafting APIs
PPTX
Developing Apps with Azure AD
PPTX
Design Summit - RESTful API Overview - John Hardy
PDF
Recipes for API Ninjas
PDF
Intro to GraphQL
PPTX
REST API for your WP7 App
PPTX
Timings API: Performance Assertion during the functional testing
REST-API introduction for developers
API Workshop: Deep dive into REST APIs
Practices and tools for building better APIs
Practices and tools for building better API (JFall 2013)
Cqrs api v2
High quality ap is with api platform
Introduction to CloudStack API
Apic dc api deep dive
Rest with Spring
Super simple introduction to REST-APIs (2nd version)
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Building Beautiful REST APIs with ASP.NET Core
Crafting APIs
Developing Apps with Azure AD
Design Summit - RESTful API Overview - John Hardy
Recipes for API Ninjas
Intro to GraphQL
REST API for your WP7 App
Timings API: Performance Assertion during the functional testing

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
cuic standard and advanced reporting.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Cloud computing and distributed systems.
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Approach and Philosophy of On baking technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
KodekX | Application Modernization Development
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Encapsulation theory and applications.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
cuic standard and advanced reporting.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Cloud computing and distributed systems.
Diabetes mellitus diagnosis method based random forest with bat algorithm
Per capita expenditure prediction using model stacking based on satellite ima...
Approach and Philosophy of On baking technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
KodekX | Application Modernization Development
The AUB Centre for AI in Media Proposal.docx
Network Security Unit 5.pdf for BCA BBA.
Building Integrated photovoltaic BIPV_UPV.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Modernizing your data center with Dell and AMD
NewMind AI Monthly Chronicles - July 2025
Spectral efficient network and resource selection model in 5G networks
Encapsulation theory and applications.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing

REST API Best Practices & Implementing in Codeigniter

  • 1. REST API & Implementing it in CodeIgniter
  • 2. Who Am I • PHP Developer & Consultant • Reviewed “Testing with Qunit” • Helped to bring out thesis on “Business Prospective of cloud computing” • Founder of Website “WebGunny.com” RIP Jun 2010 - Dec 2011
  • 3. In this talk... • Why REST API is a heart of every product • REST API – As developer UI • Best Practices of REST API • REST API in codeigniter
  • 5. Your App is not isolated
  • 6. Developer is your API customer
  • 8. Let’s Start with best practices API End Point : https://www.YourApp.com/Api/ OR https://Api.YourApp.com/ Finally Format: https://www.YourApp.com/Api/ResourceName
  • 9. Identifying resources • You can make the resources more sensible based on your product • For example – Tasks – Comments – Notifications – Users – Projects – Files
  • 11. Make Use of HTTP Verbs • GET /task - Retrieves a list of task • GET /task/12 - Retrieves a specific task • POST /task - Creates a new task • PUT /task/12 - Updates task #12 • PATCH /task/12 - Partially updates task #12 • DELETE /task/ - Deletes all task • DELETE /task/12 - Deletes task #12
  • 12. Map the relationships • GET /task/12/comments - Retrieves list of comments for task #12 • GET /task /12/comments/5 - Retrieves comment #5 for task #12 • POST /task /12/comments - Creates a new comments in task #12 • PUT /task /12/comments/5 - Updates comments #5 for task #12 • PATCH /task /12/comments/5 - Partially updates comment #5 for task #12 • DELETE /task/12/comments/5 - Deletes comment #5 for task #12
  • 13. Search Sort & Filter • GET /tasks?sort=-priority - Retrieves a list of task in descending order of priority • GET /tasks?sort=-priority,created_at - Retrieves a list of tasks in descending order of priority then by date created
  • 14. Aliases for common queries To make the API experience more pleasant for the average consumer GET /tasks?status=completed GET /tasks/recently_completed
  • 15. Allow the fields to be selected The API consumer doesn't always need the full representation of a resource. GET /task?fields=id,title,updated_at
  • 16. Paging of data Paging makes the API fast & responsive GET /notification?page=1&per_page=50
  • 17. Return full resource after action • A PUT, POST or PATCH call may make modifications to fields • Return the updated (or created) representation as part of the response. • Prevent an API consumer from having to hit the API again
  • 18. Auto loading related resources { "id" : 12, “TaskName" : "I have a question!", "summary" : "Hi, ....", "customer" : { "name" : "Bob" }, assigned_user: { "id" : 42, "name" : "Jim", } }
  • 19. Make Error Message Friendly • The API should always return sensible HTTP status codes • 400 series status codes for client issues & 500 series status codes for server issues • API should standardize that all 400 series errors come with consumable JSON error representation { "code" : 1234, "message" : “task field validation failed ", "description" : “Due date is not set" }
  • 23. REST API in Codeigniter UI/ Controller Your App Your API Rest Client
  • 24. What we need • Codeigniter • chriskacerguis/codeigniter-restserver • Router implementation
  • 25. Structuring the project /application /controller/ api/ //For all api controllers /libraries //For the third-party libraries REST_server.php Format.php /config //For all config files Router.php Rest_server.php
  • 26. Router Implementation //res/id/function/id --> res/function/id/num/sid/num $route['api/([a-z_]+)/(:any)/([a-z_]+)/(:any)'] = 'api/$1/$3/id/$2/rid/$4'; //res/id/function --> res/function/id/num $route['api/([a-z_]+)/(:any)/([a-z_]+)'] = 'api/$1/$3/id/$2'; //res/function --> res/function $route['api/([a-z_]+)/([a-z_]+)'] = 'api/$1/$2/'; //res/id --> res/index/id/num $route['api/([a-z_]+)/(:any)'] = 'api/$1/index/id/$2'; //res/ --> //res/ $route['api/([a-z_]+)'] = 'api/$1';
  • 27. Creating a first API controller require(APPPATH . '/libraries/Rest_Service.php'); class task extends REST_Service{ public function index_get() { //Logic } public function index_post() { //Logic } public function index_put() { //Logic } public function index_patch() { //Logic } public function index_delete() { //Logic } }
  • 28. Every Function has 2 Reaction public function index_get() { if($this->get('id')) { //Application Logic $this->response($results,$code); } else { //Application Logic $this->response($results,$code); } }
  • 29. HTTP Action Vs SQL • Get (select) – Get All – Get by ID • Post (Insert) • Put (update all fields ) • Patch (update selected fields) • Delete (delete) – Delete All – Delete by ID
  • 30. Summary • REST API is heart of product • REST API is a developer UI • Follow the best practices of REST API • Use “chriskacerguis/codeigniter-restserver” to implement REST in codeigniter
  • 31. Questions ? Website:SachinGKulkarni.com Twitter:@sachingk30 Email:sachingk.30@gmail.com

Editor's Notes

  • #3: I am a PHP Developer & Consultant Have reviewed “Testing with Quinit” Helped to bring out thesis on Business Prospective of cloud computing Founder of webgunny.com – A iGaming portal whose revenue is based on advertisement. However, I shutdown that site after 1.8 years of operation
  • #5: Let’s say you started building your next product – A task management system. Users can login, add tasks, assign it to people in their team, discuss on a task, change status & so on. You built it in MVC – It’s a Nice Architecture and launched it. Around 10 users started using it...Few users became a fan of your app – A loyal users They demanded the app to be on Mobile devices with good native experience. Now the problem arises. You must rewrite the business logic for ios ,android, firefox os & so on. It’s like redoing the whole app in all the devices!!! That’s wired!!! Did we had made something which would have escaped us from this problem ? Yes, you should have implemented a REST API – Representational state transfer Application Programming Interface All your application, be it a desktop app, web app, mobile app will all your API to run your business logic Once the API is being implemented you can integrate with Google Glasses, iWatches anything you name....
  • #6: Now all your loyal users are happy. You customers increased. Soon one of your customer want to integrate your app with his home grown app Now you can ask your customer to use your API to integration Of course, with some authentation & authorization
  • #7: But...here is a catch..you actual API customer is not the end user. He is a developer Now the question is to how to make the developer happy with your API Simple answer – Build a developer friendly API
  • #8: It’s definitely not a rocket science. It’s just a common sense with some intelligence
  • #12: Build your API around HTTP Action – GET, POST, PUT,PATCH,DELETE In example you can see the first one retrieves all the tasks
  • #13: Next , Map the relationship First example retrives all the comments on a task #12
  • #14: Build a mechanism to search/sort/filter You can use some common sense here and build a aliases API call For example build a separate API call to get all recently completed tasks
  • #18: Let your API return full details about the task on every operation. Say you updated a task by calling a API request. Instead of just return Boolean if the activity is successful Just return full details of the task This will reduce the work of the developer to hit the API gain to get the updated data
  • #19: It’s a best practice to related data. For example, instead of just returning the assigned user ID It Return the whole user object/resource along with the user name. This will also reduce the number of hits to API server
  • #20: Make errors friendly to understand. API should always return sensible HTTP status code 200 – For successfully operation 400 – For errors data validation etc 500 – For server errors Also return the standard error object with the proper message stating why the operation failed. It would be good if you can provide a code for all your errors.