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

What's hot

Spring Boot
Spring BootSpring Boot
Spring Boot
Jiayun Zhou
 
Alfresco勉強会#33 alfresco 5.1でコンテンツ自動削除を実装してみた
Alfresco勉強会#33 alfresco 5.1でコンテンツ自動削除を実装してみたAlfresco勉強会#33 alfresco 5.1でコンテンツ自動削除を実装してみた
Alfresco勉強会#33 alfresco 5.1でコンテンツ自動削除を実装してみた
Tasuku Otani
 
Fake It Outside-In TDD @XP2017
Fake It Outside-In TDD @XP2017Fake It Outside-In TDD @XP2017
Fake It Outside-In TDD @XP2017
David Völkel
 
Java 8-streams-collectors-patterns
Java 8-streams-collectors-patternsJava 8-streams-collectors-patterns
Java 8-streams-collectors-patterns
José Paumard
 
Spring Framework - MVC
Spring Framework - MVCSpring Framework - MVC
Spring Framework - MVC
Dzmitry Naskou
 
L'API Collector dans tous ses états
L'API Collector dans tous ses étatsL'API Collector dans tous ses états
L'API Collector dans tous ses états
José Paumard
 
Java servlets
Java servletsJava servlets
Java servlets
yuvarani p
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
tola99
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
Joshua Long
 
Laravel
LaravelLaravel
Laravel
tanveerkhan62
 
Spring Security 5
Spring Security 5Spring Security 5
Spring Security 5
Jesus Perez Franco
 
REST API
REST APIREST API
REST API
Tofazzal Ahmed
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
NewCircle Training
 
REST API Design & Development
REST API Design & DevelopmentREST API Design & Development
REST API Design & Development
Ashok Pundit
 
Testing API platform with Behat BDD tests
Testing API platform with Behat BDD testsTesting API platform with Behat BDD tests
Testing API platform with Behat BDD tests
Stefan Adolf
 
Spring MVC 3.0 Framework
Spring MVC 3.0 FrameworkSpring MVC 3.0 Framework
Transactions and Concurrency Control Patterns
Transactions and Concurrency Control PatternsTransactions and Concurrency Control Patterns
Transactions and Concurrency Control Patterns
J On The Beach
 
REST API and CRUD
REST API and CRUDREST API and CRUD
REST API and CRUD
Prem Sanil
 

What's hot (20)

Spring Boot
Spring BootSpring Boot
Spring Boot
 
Alfresco勉強会#33 alfresco 5.1でコンテンツ自動削除を実装してみた
Alfresco勉強会#33 alfresco 5.1でコンテンツ自動削除を実装してみたAlfresco勉強会#33 alfresco 5.1でコンテンツ自動削除を実装してみた
Alfresco勉強会#33 alfresco 5.1でコンテンツ自動削除を実装してみた
 
Fake It Outside-In TDD @XP2017
Fake It Outside-In TDD @XP2017Fake It Outside-In TDD @XP2017
Fake It Outside-In TDD @XP2017
 
Java 8-streams-collectors-patterns
Java 8-streams-collectors-patternsJava 8-streams-collectors-patterns
Java 8-streams-collectors-patterns
 
Spring Framework - MVC
Spring Framework - MVCSpring Framework - MVC
Spring Framework - MVC
 
L'API Collector dans tous ses états
L'API Collector dans tous ses étatsL'API Collector dans tous ses états
L'API Collector dans tous ses états
 
Tomcat
TomcatTomcat
Tomcat
 
Java servlets
Java servletsJava servlets
Java servlets
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
 
Laravel
LaravelLaravel
Laravel
 
Spring Security 5
Spring Security 5Spring Security 5
Spring Security 5
 
REST API
REST APIREST API
REST API
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
 
REST API Design & Development
REST API Design & DevelopmentREST API Design & Development
REST API Design & Development
 
Testing API platform with Behat BDD tests
Testing API platform with Behat BDD testsTesting API platform with Behat BDD tests
Testing API platform with Behat BDD tests
 
Spring MVC 3.0 Framework
Spring MVC 3.0 FrameworkSpring MVC 3.0 Framework
Spring MVC 3.0 Framework
 
Transactions and Concurrency Control Patterns
Transactions and Concurrency Control PatternsTransactions and Concurrency Control Patterns
Transactions and Concurrency Control Patterns
 
REST API and CRUD
REST API and CRUDREST API and CRUD
REST API and CRUD
 
Codeigniter
CodeigniterCodeigniter
Codeigniter
 

Viewers also liked

Web Services PHP Tutorial
Web Services PHP TutorialWeb Services PHP Tutorial
Web Services PHP Tutorial
Lorna Mitchell
 
Consuming RESTful services in PHP
Consuming RESTful services in PHPConsuming RESTful services in PHP
Consuming RESTful services in PHPZoran Jeremic
 
Advanced Web Services Hacking (AusCERT 06)
Advanced Web Services Hacking (AusCERT 06)Advanced Web Services Hacking (AusCERT 06)
Advanced Web Services Hacking (AusCERT 06)
Shreeraj Shah
 
ACL in CodeIgniter
ACL in CodeIgniterACL in CodeIgniter
ACL in CodeIgniter
mirahman
 
PHP and Web Services
PHP and Web ServicesPHP and Web Services
PHP and Web Services
Bruno Pedro
 
Secure Your REST API (The Right Way)
Secure Your REST API (The Right Way)Secure Your REST API (The Right Way)
Secure Your REST API (The Right Way)
Stormpath
 
Develop webservice in PHP
Develop webservice in PHPDevelop webservice in PHP
Develop webservice in PHP
Sanil Subhash Chandra Bose
 
Creating And Consuming Web Services In Php 5
Creating And Consuming Web Services In Php 5Creating And Consuming Web Services In Php 5
Creating And Consuming Web Services In Php 5Michael Girouard
 
REST API Doc Best Practices
REST API Doc Best PracticesREST API Doc Best Practices
REST API Doc Best Practices
Marta Rauch
 
You must know about CodeIgniter Popular Library
You must know about CodeIgniter Popular LibraryYou must know about CodeIgniter Popular Library
You must know about CodeIgniter Popular Library
Bo-Yi Wu
 
Servicio y Consumo de Servicios REST en PHP
Servicio y Consumo de Servicios REST en PHPServicio y Consumo de Servicios REST en PHP
Servicio y Consumo de Servicios REST en PHP
David J. Brenes
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC Framework
Bo-Yi Wu
 
Criando e consumindo webservice REST com PHP e JSON
Criando e consumindo webservice REST com PHP e JSONCriando e consumindo webservice REST com PHP e JSON
Criando e consumindo webservice REST com PHP e JSON
Marcio Junior Vieira
 
REST & RESTful Web Services
REST & RESTful Web ServicesREST & RESTful Web Services
REST & RESTful Web Services
Halil Burak Cetinkaya
 
Design Beautiful REST + JSON APIs
Design Beautiful REST + JSON APIsDesign Beautiful REST + JSON APIs
Design Beautiful REST + JSON APIs
Stormpath
 
CodeIgniter L3 model & active record & template
CodeIgniter L3 model & active record  & templateCodeIgniter L3 model & active record  & template
CodeIgniter L3 model & active record & template
Mohammad Tahsin Alshalabi
 
Api details for american syscorp
Api details for american syscorpApi details for american syscorp
Api details for american syscorp
Carmor Bass
 
Modular PHP Development using CodeIgniter Bonfire
Modular PHP Development using CodeIgniter BonfireModular PHP Development using CodeIgniter Bonfire
Modular PHP Development using CodeIgniter Bonfire
Jeff Fox
 
Codeigniter : the security and the magic of hook
Codeigniter : the security and the magic of hookCodeigniter : the security and the magic of hook
Codeigniter : the security and the magic of hook
Abdul Malik Ikhsan
 

Viewers also liked (20)

Web Services PHP Tutorial
Web Services PHP TutorialWeb Services PHP Tutorial
Web Services PHP Tutorial
 
Consuming RESTful services in PHP
Consuming RESTful services in PHPConsuming RESTful services in PHP
Consuming RESTful services in PHP
 
CodeIgniter 3.0
CodeIgniter 3.0CodeIgniter 3.0
CodeIgniter 3.0
 
Advanced Web Services Hacking (AusCERT 06)
Advanced Web Services Hacking (AusCERT 06)Advanced Web Services Hacking (AusCERT 06)
Advanced Web Services Hacking (AusCERT 06)
 
ACL in CodeIgniter
ACL in CodeIgniterACL in CodeIgniter
ACL in CodeIgniter
 
PHP and Web Services
PHP and Web ServicesPHP and Web Services
PHP and Web Services
 
Secure Your REST API (The Right Way)
Secure Your REST API (The Right Way)Secure Your REST API (The Right Way)
Secure Your REST API (The Right Way)
 
Develop webservice in PHP
Develop webservice in PHPDevelop webservice in PHP
Develop webservice in PHP
 
Creating And Consuming Web Services In Php 5
Creating And Consuming Web Services In Php 5Creating And Consuming Web Services In Php 5
Creating And Consuming Web Services In Php 5
 
REST API Doc Best Practices
REST API Doc Best PracticesREST API Doc Best Practices
REST API Doc Best Practices
 
You must know about CodeIgniter Popular Library
You must know about CodeIgniter Popular LibraryYou must know about CodeIgniter Popular Library
You must know about CodeIgniter Popular Library
 
Servicio y Consumo de Servicios REST en PHP
Servicio y Consumo de Servicios REST en PHPServicio y Consumo de Servicios REST en PHP
Servicio y Consumo de Servicios REST en PHP
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC Framework
 
Criando e consumindo webservice REST com PHP e JSON
Criando e consumindo webservice REST com PHP e JSONCriando e consumindo webservice REST com PHP e JSON
Criando e consumindo webservice REST com PHP e JSON
 
REST & RESTful Web Services
REST & RESTful Web ServicesREST & RESTful Web Services
REST & RESTful Web Services
 
Design Beautiful REST + JSON APIs
Design Beautiful REST + JSON APIsDesign Beautiful REST + JSON APIs
Design Beautiful REST + JSON APIs
 
CodeIgniter L3 model & active record & template
CodeIgniter L3 model & active record  & templateCodeIgniter L3 model & active record  & template
CodeIgniter L3 model & active record & template
 
Api details for american syscorp
Api details for american syscorpApi details for american syscorp
Api details for american syscorp
 
Modular PHP Development using CodeIgniter Bonfire
Modular PHP Development using CodeIgniter BonfireModular PHP Development using CodeIgniter Bonfire
Modular PHP Development using CodeIgniter Bonfire
 
Codeigniter : the security and the magic of hook
Codeigniter : the security and the magic of hookCodeigniter : the security and the magic of hook
Codeigniter : the security and the magic of hook
 

Similar to REST API Best Practices & Implementing in Codeigniter

REST-API introduction for developers
REST-API introduction for developersREST-API introduction for developers
REST-API introduction for developers
Patrick Savalle
 
API Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIsAPI Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIs
Tom Johnson
 
Practices and tools for building better APIs
Practices and tools for building better APIsPractices and tools for building better APIs
Practices and tools for building better APIsNLJUG
 
Practices and tools for building better API (JFall 2013)
Practices and tools for building better API (JFall 2013)Practices and tools for building better API (JFall 2013)
Practices and tools for building better API (JFall 2013)
Peter Hendriks
 
REST APIs
REST APIsREST APIs
High quality ap is with api platform
High quality ap is with api platformHigh quality ap is with api platform
High quality ap is with api platform
Nelson Kopliku
 
Introduction to CloudStack API
Introduction to CloudStack APIIntroduction to CloudStack API
Introduction to CloudStack API
Krunal Jain
 
Apic dc api deep dive
Apic dc api deep dive Apic dc api deep dive
Apic dc api deep dive
Cisco DevNet
 
Super simple introduction to REST-APIs (2nd version)
Super simple introduction to REST-APIs (2nd version)Super simple introduction to REST-APIs (2nd version)
Super simple introduction to REST-APIs (2nd version)
Patrick Savalle
 
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram VaswaniCreating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
vvaswani
 
Building Beautiful REST APIs with ASP.NET Core
Building Beautiful REST APIs with ASP.NET CoreBuilding Beautiful REST APIs with ASP.NET Core
Building Beautiful REST APIs with ASP.NET Core
Stormpath
 
Crafting APIs
Crafting APIsCrafting APIs
Crafting APIs
Tatiana Al-Chueyr
 
Developing Apps with Azure AD
Developing Apps with Azure ADDeveloping Apps with Azure AD
Developing Apps with Azure AD
SharePointRadi
 
AWS July Webinar Series: Overview: Build and Manage your APIs with Amazon API...
AWS July Webinar Series: Overview: Build and Manage your APIs with Amazon API...AWS July Webinar Series: Overview: Build and Manage your APIs with Amazon API...
AWS July Webinar Series: Overview: Build and Manage your APIs with Amazon API...
Amazon Web Services
 
AWS July Webinar Series - Overview Build and Manage your APs with amazon api ...
AWS July Webinar Series - Overview Build and Manage your APs with amazon api ...AWS July Webinar Series - Overview Build and Manage your APs with amazon api ...
AWS July Webinar Series - Overview Build and Manage your APs with amazon api ...Amazon Web Services
 
Design Summit - RESTful API Overview - John Hardy
Design Summit - RESTful API Overview - John HardyDesign Summit - RESTful API Overview - John Hardy
Design Summit - RESTful API Overview - John Hardy
ManageIQ
 
Recipes for API Ninjas
Recipes for API NinjasRecipes for API Ninjas
Recipes for API Ninjas
Nordic APIs
 
Intro to GraphQL
 Intro to GraphQL Intro to GraphQL
Intro to GraphQL
Rakuten Group, Inc.
 

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

REST-API introduction for developers
REST-API introduction for developersREST-API introduction for developers
REST-API introduction for developers
 
API Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIsAPI Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIs
 
Practices and tools for building better APIs
Practices and tools for building better APIsPractices and tools for building better APIs
Practices and tools for building better APIs
 
Practices and tools for building better API (JFall 2013)
Practices and tools for building better API (JFall 2013)Practices and tools for building better API (JFall 2013)
Practices and tools for building better API (JFall 2013)
 
REST APIs
REST APIsREST APIs
REST APIs
 
Cqrs api v2
Cqrs api v2Cqrs api v2
Cqrs api v2
 
High quality ap is with api platform
High quality ap is with api platformHigh quality ap is with api platform
High quality ap is with api platform
 
Introduction to CloudStack API
Introduction to CloudStack APIIntroduction to CloudStack API
Introduction to CloudStack API
 
Apic dc api deep dive
Apic dc api deep dive Apic dc api deep dive
Apic dc api deep dive
 
Rest with Spring
Rest with SpringRest with Spring
Rest with Spring
 
Super simple introduction to REST-APIs (2nd version)
Super simple introduction to REST-APIs (2nd version)Super simple introduction to REST-APIs (2nd version)
Super simple introduction to REST-APIs (2nd version)
 
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram VaswaniCreating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
 
Building Beautiful REST APIs with ASP.NET Core
Building Beautiful REST APIs with ASP.NET CoreBuilding Beautiful REST APIs with ASP.NET Core
Building Beautiful REST APIs with ASP.NET Core
 
Crafting APIs
Crafting APIsCrafting APIs
Crafting APIs
 
Developing Apps with Azure AD
Developing Apps with Azure ADDeveloping Apps with Azure AD
Developing Apps with Azure AD
 
AWS July Webinar Series: Overview: Build and Manage your APIs with Amazon API...
AWS July Webinar Series: Overview: Build and Manage your APIs with Amazon API...AWS July Webinar Series: Overview: Build and Manage your APIs with Amazon API...
AWS July Webinar Series: Overview: Build and Manage your APIs with Amazon API...
 
AWS July Webinar Series - Overview Build and Manage your APs with amazon api ...
AWS July Webinar Series - Overview Build and Manage your APs with amazon api ...AWS July Webinar Series - Overview Build and Manage your APs with amazon api ...
AWS July Webinar Series - Overview Build and Manage your APs with amazon api ...
 
Design Summit - RESTful API Overview - John Hardy
Design Summit - RESTful API Overview - John HardyDesign Summit - RESTful API Overview - John Hardy
Design Summit - RESTful API Overview - John Hardy
 
Recipes for API Ninjas
Recipes for API NinjasRecipes for API Ninjas
Recipes for API Ninjas
 
Intro to GraphQL
 Intro to GraphQL Intro to GraphQL
Intro to GraphQL
 

Recently uploaded

IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
Abida Shariff
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
CatarinaPereira64715
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 

Recently uploaded (20)

IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 

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

  1. 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
  2. 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....
  3. 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
  4. 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
  5. It’s definitely not a rocket science. It’s just a common sense with some intelligence
  6. Build your API around HTTP Action – GET, POST, PUT,PATCH,DELETE In example you can see the first one retrieves all the tasks
  7. Next , Map the relationship First example retrives all the comments on a task #12
  8. 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
  9. 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
  10. 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
  11. 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.