SlideShare ist ein Scribd-Unternehmen logo
1 von 29
LARAVEL PHP SERVER SIDE RESTFUL
API BEST PRACTICES
Name: Vu Quang Son
SERVER SIDE
2016
1
Table of content
2
 Versioning
 Routing
 Filter, sort, search, paging
 Json format
 HTTP Status Code
 Other best practices
Versioning
 /api/v1
 /public/api/v1/apps
3
Versioning (Best Practices)
 API Version is always required
 Use simple number (1, 2, …) & avoid dot such as 2.5
 Versioning starting with the letter “v”
4
Routing
5
Routing (cont)
1. GET /answers - Retrieves a list of answers
2. GET /answers/12 - Retrieves a specific answer
3. POST /answers - Creates a new answers
4. PUT /answers/12 - Updates answer #12
5. DELETE /answers/12 - Deletes answer #12
6
Implement Routing
7
Routing
(Advantages & Best Practices)
 Apply existing HTTP Methods to implement multiple
functions on just single /answers endpoint
 No naming conventions to follow and URL is clean &
clear
 Use nouns not verbs
 Use only plural nouns
8
Routing
(Discussion)
 How about custom routes?
 GET /apps/filter
 GET /apps/related
 How about routes with multiple words
 Use dashes ( - ) for words delimiter
 Deal with multiple objects
 POST /answers/create
 PUT /answers/edit
 DELETE /answers/remove 9
Routing
(Discussion)
 Deal with relations?
 GET /apps/12/questions
 GET /questions?app_id=12
 GET /apps/12/questions/14/medias
 GET /medias?app_id=12&question_id=14
10
FILTER, SORT, SEARCH, PAGING
11
 FILTER
 Use unique query parameter for each field that
implements filtering
 Use database fields for faster implementation
 GET /apps?status=draft
 GET /apps?status=published&featured=1
FILTER, SORT, SEARCH, PAGING
12
 FILTER (Discussion & Improvement)
 The best if can also filter with most used parameters
 &gt, &lt, &gte, &lte, …
 GET /apps?rating[value]=2&rating[operator]="&gte“
 GET /apps?price[value]=0&price[operator]="&gt“
 GET /apps?has_price=1
FILTER, SORT, SEARCH, PAGING
13
 SORT
 Defined constant sort
 Parameters delimiter by comma (,)
 -created_at for DESC
 create_at for ASC
 GET /apps?sort=-created_at,id
FILTER, SORT, SEARCH, PAGING
14
 SEARCH
 Defined constant search (search or q?)
 GET /apps?search=“IBM test”
 GET /apps?q=“IBM test”
FILTER, SORT, SEARCH, PAGING
15
 SEARCH (Discussion & Improvement)
 search or q keyword?
 GET /apps?search=“IBM test”
 GET /apps?q=“IBM test”
 GET /apps?q[value]=“IBM”&q[field]=“title”
FILTER, SORT, SEARCH, PAGING
16
 PAGING
 Defined constant limit and offset
 Default limit = 10 & offset = 0
 /apps?limit=20&offset=10
 Want no limit?
 /apps?limit=
 /apps?limit=0
FILTER, SORT, SEARCH, PAGING
17
 PAGING
 Defined constant limit and offset
 Default limit = 10 & offset = 0
 /apps?limit=20&offset=10
 Want no limit?
 /apps?limit=
 /apps?limit=0
FILTER, SORT, SEARCH, PAGING
18
 Limit fields returned by API
 Defined constant fields
 GET /apps?fields=id,title,created_at
JSON FORMAT (Success)
19
{
"errorCode": null,
"message": null,
"result": [ ]
}
{
"errorCode": null,
"message": null,
"result": { }
}
JSON FORMAT (Error)
20
{
"errorCode": "validation_error",
"message": [
“The selected icon is invalid.”,
“The icon is invalid or in used”
],
"result": null
}
JSON FORMAT (Error)
21
{
"errorCode": "validation_error",
"message": {
"icon": [
"The selected icon is invalid."
],
"background": [
"The selected background is invalid."
]
},
"result": null
}
AVOID BAD PRACTICE
22
{
"errorCode": "validation_error",
"message": null,
"result": [
1: { },
2: { }
]
}
HTTP STATUS CODE
23
 200 OK – successful GET, PUT, DELETE
 201 Created – successful POST in creation
 204 No Content – successful request like DELETE
 304 Not Modified – for caching
 400 Bad Request – malformed request, cannot parse
 401 Unauthorized – invalid authentication
 403 Forbidden – do not have access
 404 Not Found – resource doesn’t exist
 405 Method Not Allowed – not implemented/not allow
 412 Precondition Failed – validation header
 422 Unprocessable Entity – validation body
 429 Too Many Requests – reject due to rate limit
 500 Internal Server Error – server error
HTTP STATUS CODE
(Discussion & Improvement)
24
 Using 201 Created – for successful POST in creation
instead of 200 OK
 Using 422 Unprocessable Entity – for validation error
instead of 412 Precondition Failed
OTHER BEST PRACTICES
25
 Using json only for response
OTHER BEST PRACTICES
26
 Always enable Gzip for api
 Handle Cors (Coss-Origin Resource Sharing)
 Allow overriding HTTP method (X-HTTP-Method-
Override)
REFERENCE
27
 http://www.vinaysahni.com/best-practices-for-a-
pragmatic-restful-api
 https://laravel.com/docs/5.3/controllers
 http://blog.mwaysolutions.com/2014/06/05/10-
best-practices-for-better-restful-api/
 https://github.com/FriendsOfCake/crud/issues/337
 https://saipraveenblog.wordpress.com/2014/09/29/
rest-api-best-practices/
Q & A
28
2929

Weitere ähnliche Inhalte

Was ist angesagt?

Django Framework and Application Structure
Django Framework and Application StructureDjango Framework and Application Structure
Django Framework and Application StructureSEONGTAEK OH
 
The never-ending REST API design debate
The never-ending REST API design debateThe never-ending REST API design debate
The never-ending REST API design debateRestlet
 
Html 5 in a big nutshell
Html 5 in a big nutshellHtml 5 in a big nutshell
Html 5 in a big nutshellLennart Schoors
 
6 global library function provided by open cart
6 global library function provided by open cart6 global library function provided by open cart
6 global library function provided by open cartSelf
 
Django Introduction Osscamp Delhi September 08 09 2007 Mir Nazim
Django Introduction Osscamp Delhi September 08 09 2007 Mir NazimDjango Introduction Osscamp Delhi September 08 09 2007 Mir Nazim
Django Introduction Osscamp Delhi September 08 09 2007 Mir NazimMir Nazim
 
Escape from the automation hell
Escape from the automation hellEscape from the automation hell
Escape from the automation hellNikita Simonovets
 
APIs REST Usables con Hypermedia por Javier Ramirez, para codemotion
APIs REST Usables con Hypermedia por Javier Ramirez, para codemotionAPIs REST Usables con Hypermedia por Javier Ramirez, para codemotion
APIs REST Usables con Hypermedia por Javier Ramirez, para codemotionjavier ramirez
 
Introduction to RESTful Web Services
Introduction to RESTful Web ServicesIntroduction to RESTful Web Services
Introduction to RESTful Web ServicesFelipe Dornelas
 
Mastering the Oracle Data Pump API
Mastering the Oracle Data Pump APIMastering the Oracle Data Pump API
Mastering the Oracle Data Pump APIEnkitec
 
Why I Love JSX!
Why I Love JSX!Why I Love JSX!
Why I Love JSX!Jay Phelps
 
Web development with django - Basics Presentation
Web development with django - Basics PresentationWeb development with django - Basics Presentation
Web development with django - Basics PresentationShrinath Shenoy
 
How to build a rest api.pptx
How to build a rest api.pptxHow to build a rest api.pptx
How to build a rest api.pptxHarry Potter
 
Childthemes ottawa-word camp-1919
Childthemes ottawa-word camp-1919Childthemes ottawa-word camp-1919
Childthemes ottawa-word camp-1919Paul Bearne
 
Tour of sh404SEF - SEO and security for Joomla
Tour of sh404SEF - SEO and security for JoomlaTour of sh404SEF - SEO and security for Joomla
Tour of sh404SEF - SEO and security for Joomlavdrover
 

Was ist angesagt? (17)

Django Framework and Application Structure
Django Framework and Application StructureDjango Framework and Application Structure
Django Framework and Application Structure
 
The never-ending REST API design debate
The never-ending REST API design debateThe never-ending REST API design debate
The never-ending REST API design debate
 
Html 5 in a big nutshell
Html 5 in a big nutshellHtml 5 in a big nutshell
Html 5 in a big nutshell
 
6 global library function provided by open cart
6 global library function provided by open cart6 global library function provided by open cart
6 global library function provided by open cart
 
Django Introduction Osscamp Delhi September 08 09 2007 Mir Nazim
Django Introduction Osscamp Delhi September 08 09 2007 Mir NazimDjango Introduction Osscamp Delhi September 08 09 2007 Mir Nazim
Django Introduction Osscamp Delhi September 08 09 2007 Mir Nazim
 
Escape from the automation hell
Escape from the automation hellEscape from the automation hell
Escape from the automation hell
 
APIs REST Usables con Hypermedia por Javier Ramirez, para codemotion
APIs REST Usables con Hypermedia por Javier Ramirez, para codemotionAPIs REST Usables con Hypermedia por Javier Ramirez, para codemotion
APIs REST Usables con Hypermedia por Javier Ramirez, para codemotion
 
Introduction to RESTful Web Services
Introduction to RESTful Web ServicesIntroduction to RESTful Web Services
Introduction to RESTful Web Services
 
Mastering the Oracle Data Pump API
Mastering the Oracle Data Pump APIMastering the Oracle Data Pump API
Mastering the Oracle Data Pump API
 
Why I Love JSX!
Why I Love JSX!Why I Love JSX!
Why I Love JSX!
 
Web development with django - Basics Presentation
Web development with django - Basics PresentationWeb development with django - Basics Presentation
Web development with django - Basics Presentation
 
Reliable acceptance testing
Reliable acceptance testingReliable acceptance testing
Reliable acceptance testing
 
Slide Sahre
Slide SahreSlide Sahre
Slide Sahre
 
How to build a rest api.pptx
How to build a rest api.pptxHow to build a rest api.pptx
How to build a rest api.pptx
 
Childthemes ottawa-word camp-1919
Childthemes ottawa-word camp-1919Childthemes ottawa-word camp-1919
Childthemes ottawa-word camp-1919
 
Tour of sh404SEF - SEO and security for Joomla
Tour of sh404SEF - SEO and security for JoomlaTour of sh404SEF - SEO and security for Joomla
Tour of sh404SEF - SEO and security for Joomla
 
Php frameworks
Php frameworksPhp frameworks
Php frameworks
 

Ähnlich wie PHP Server side restful API - linkedin

APIdays Helsinki 2019 - API Versioning with REST, JSON and Swagger with Thoma...
APIdays Helsinki 2019 - API Versioning with REST, JSON and Swagger with Thoma...APIdays Helsinki 2019 - API Versioning with REST, JSON and Swagger with Thoma...
APIdays Helsinki 2019 - API Versioning with REST, JSON and Swagger with Thoma...apidays
 
Great APIs - Future of Your Progress App
Great APIs - Future of Your Progress AppGreat APIs - Future of Your Progress App
Great APIs - Future of Your Progress AppGabriel Lucaciu
 
[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기
[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기
[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기lanslote
 
Web API Test Automation using Frisby & Node.js
Web API Test Automation using Frisby  & Node.jsWeb API Test Automation using Frisby  & Node.js
Web API Test Automation using Frisby & Node.jsChi Lang Le Vu Tran
 
SharePoint 2010 Application Development Overview
SharePoint 2010 Application Development OverviewSharePoint 2010 Application Development Overview
SharePoint 2010 Application Development OverviewRob Windsor
 
En story of cakephp2.0
En story of cakephp2.0En story of cakephp2.0
En story of cakephp2.0Hiroki Shimizu
 
Deep dive into feature versioning and upgrade support in SharePoint 2010
Deep dive into feature versioning and upgrade support in SharePoint 2010Deep dive into feature versioning and upgrade support in SharePoint 2010
Deep dive into feature versioning and upgrade support in SharePoint 2010Jeremy Thake
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API07.pallav
 
Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.Mario Cardinal
 
Deep dive into feature versioning in SharePoint 2010
Deep dive into feature versioning in SharePoint 2010Deep dive into feature versioning in SharePoint 2010
Deep dive into feature versioning in SharePoint 2010Jeremy Thake
 
SPTechCon 2014 How to develop and debug client side code in SharePoint
SPTechCon 2014 How to develop and debug client side code in SharePointSPTechCon 2014 How to develop and debug client side code in SharePoint
SPTechCon 2014 How to develop and debug client side code in SharePointMark Rackley
 
03 form-data
03 form-data03 form-data
03 form-datasnopteck
 
Taking Advantage of the SharePoint 2013 REST API
Taking Advantage of the SharePoint 2013 REST APITaking Advantage of the SharePoint 2013 REST API
Taking Advantage of the SharePoint 2013 REST APIEric Shupps
 
New World Of SharePoint 2010 Administration Oleson
New World Of SharePoint 2010 Administration OlesonNew World Of SharePoint 2010 Administration Oleson
New World Of SharePoint 2010 Administration OlesonJoel Oleson
 
...and thus your forms automagically disappeared
...and thus your forms automagically disappeared...and thus your forms automagically disappeared
...and thus your forms automagically disappearedLuc Bors
 

Ähnlich wie PHP Server side restful API - linkedin (20)

APIdays Helsinki 2019 - API Versioning with REST, JSON and Swagger with Thoma...
APIdays Helsinki 2019 - API Versioning with REST, JSON and Swagger with Thoma...APIdays Helsinki 2019 - API Versioning with REST, JSON and Swagger with Thoma...
APIdays Helsinki 2019 - API Versioning with REST, JSON and Swagger with Thoma...
 
Great APIs - Future of Your Progress App
Great APIs - Future of Your Progress AppGreat APIs - Future of Your Progress App
Great APIs - Future of Your Progress App
 
Android networking-2
Android networking-2Android networking-2
Android networking-2
 
[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기
[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기
[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기
 
Restful design at work v2.0
Restful design at work v2.0Restful design at work v2.0
Restful design at work v2.0
 
Web API Test Automation using Frisby & Node.js
Web API Test Automation using Frisby  & Node.jsWeb API Test Automation using Frisby  & Node.js
Web API Test Automation using Frisby & Node.js
 
Web API Test Automation Using Frisby & Node.js
Web API Test Automation Using Frisby  & Node.jsWeb API Test Automation Using Frisby  & Node.js
Web API Test Automation Using Frisby & Node.js
 
Rest API Design Rules
Rest API Design RulesRest API Design Rules
Rest API Design Rules
 
SharePoint 2010 Application Development Overview
SharePoint 2010 Application Development OverviewSharePoint 2010 Application Development Overview
SharePoint 2010 Application Development Overview
 
En story of cakephp2.0
En story of cakephp2.0En story of cakephp2.0
En story of cakephp2.0
 
Deep dive into feature versioning and upgrade support in SharePoint 2010
Deep dive into feature versioning and upgrade support in SharePoint 2010Deep dive into feature versioning and upgrade support in SharePoint 2010
Deep dive into feature versioning and upgrade support in SharePoint 2010
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
 
Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.
 
Deep dive into feature versioning in SharePoint 2010
Deep dive into feature versioning in SharePoint 2010Deep dive into feature versioning in SharePoint 2010
Deep dive into feature versioning in SharePoint 2010
 
SPTechCon 2014 How to develop and debug client side code in SharePoint
SPTechCon 2014 How to develop and debug client side code in SharePointSPTechCon 2014 How to develop and debug client side code in SharePoint
SPTechCon 2014 How to develop and debug client side code in SharePoint
 
03 form-data
03 form-data03 form-data
03 form-data
 
Taking Advantage of the SharePoint 2013 REST API
Taking Advantage of the SharePoint 2013 REST APITaking Advantage of the SharePoint 2013 REST API
Taking Advantage of the SharePoint 2013 REST API
 
API Design Tour: Dell
API Design Tour: DellAPI Design Tour: Dell
API Design Tour: Dell
 
New World Of SharePoint 2010 Administration Oleson
New World Of SharePoint 2010 Administration OlesonNew World Of SharePoint 2010 Administration Oleson
New World Of SharePoint 2010 Administration Oleson
 
...and thus your forms automagically disappeared
...and thus your forms automagically disappeared...and thus your forms automagically disappeared
...and thus your forms automagically disappeared
 

PHP Server side restful API - linkedin

  • 1. LARAVEL PHP SERVER SIDE RESTFUL API BEST PRACTICES Name: Vu Quang Son SERVER SIDE 2016 1
  • 2. Table of content 2  Versioning  Routing  Filter, sort, search, paging  Json format  HTTP Status Code  Other best practices
  • 4. Versioning (Best Practices)  API Version is always required  Use simple number (1, 2, …) & avoid dot such as 2.5  Versioning starting with the letter “v” 4
  • 6. Routing (cont) 1. GET /answers - Retrieves a list of answers 2. GET /answers/12 - Retrieves a specific answer 3. POST /answers - Creates a new answers 4. PUT /answers/12 - Updates answer #12 5. DELETE /answers/12 - Deletes answer #12 6
  • 8. Routing (Advantages & Best Practices)  Apply existing HTTP Methods to implement multiple functions on just single /answers endpoint  No naming conventions to follow and URL is clean & clear  Use nouns not verbs  Use only plural nouns 8
  • 9. Routing (Discussion)  How about custom routes?  GET /apps/filter  GET /apps/related  How about routes with multiple words  Use dashes ( - ) for words delimiter  Deal with multiple objects  POST /answers/create  PUT /answers/edit  DELETE /answers/remove 9
  • 10. Routing (Discussion)  Deal with relations?  GET /apps/12/questions  GET /questions?app_id=12  GET /apps/12/questions/14/medias  GET /medias?app_id=12&question_id=14 10
  • 11. FILTER, SORT, SEARCH, PAGING 11  FILTER  Use unique query parameter for each field that implements filtering  Use database fields for faster implementation  GET /apps?status=draft  GET /apps?status=published&featured=1
  • 12. FILTER, SORT, SEARCH, PAGING 12  FILTER (Discussion & Improvement)  The best if can also filter with most used parameters  &gt, &lt, &gte, &lte, …  GET /apps?rating[value]=2&rating[operator]="&gte“  GET /apps?price[value]=0&price[operator]="&gt“  GET /apps?has_price=1
  • 13. FILTER, SORT, SEARCH, PAGING 13  SORT  Defined constant sort  Parameters delimiter by comma (,)  -created_at for DESC  create_at for ASC  GET /apps?sort=-created_at,id
  • 14. FILTER, SORT, SEARCH, PAGING 14  SEARCH  Defined constant search (search or q?)  GET /apps?search=“IBM test”  GET /apps?q=“IBM test”
  • 15. FILTER, SORT, SEARCH, PAGING 15  SEARCH (Discussion & Improvement)  search or q keyword?  GET /apps?search=“IBM test”  GET /apps?q=“IBM test”  GET /apps?q[value]=“IBM”&q[field]=“title”
  • 16. FILTER, SORT, SEARCH, PAGING 16  PAGING  Defined constant limit and offset  Default limit = 10 & offset = 0  /apps?limit=20&offset=10  Want no limit?  /apps?limit=  /apps?limit=0
  • 17. FILTER, SORT, SEARCH, PAGING 17  PAGING  Defined constant limit and offset  Default limit = 10 & offset = 0  /apps?limit=20&offset=10  Want no limit?  /apps?limit=  /apps?limit=0
  • 18. FILTER, SORT, SEARCH, PAGING 18  Limit fields returned by API  Defined constant fields  GET /apps?fields=id,title,created_at
  • 19. JSON FORMAT (Success) 19 { "errorCode": null, "message": null, "result": [ ] } { "errorCode": null, "message": null, "result": { } }
  • 20. JSON FORMAT (Error) 20 { "errorCode": "validation_error", "message": [ “The selected icon is invalid.”, “The icon is invalid or in used” ], "result": null }
  • 21. JSON FORMAT (Error) 21 { "errorCode": "validation_error", "message": { "icon": [ "The selected icon is invalid." ], "background": [ "The selected background is invalid." ] }, "result": null }
  • 22. AVOID BAD PRACTICE 22 { "errorCode": "validation_error", "message": null, "result": [ 1: { }, 2: { } ] }
  • 23. HTTP STATUS CODE 23  200 OK – successful GET, PUT, DELETE  201 Created – successful POST in creation  204 No Content – successful request like DELETE  304 Not Modified – for caching  400 Bad Request – malformed request, cannot parse  401 Unauthorized – invalid authentication  403 Forbidden – do not have access  404 Not Found – resource doesn’t exist  405 Method Not Allowed – not implemented/not allow  412 Precondition Failed – validation header  422 Unprocessable Entity – validation body  429 Too Many Requests – reject due to rate limit  500 Internal Server Error – server error
  • 24. HTTP STATUS CODE (Discussion & Improvement) 24  Using 201 Created – for successful POST in creation instead of 200 OK  Using 422 Unprocessable Entity – for validation error instead of 412 Precondition Failed
  • 25. OTHER BEST PRACTICES 25  Using json only for response
  • 26. OTHER BEST PRACTICES 26  Always enable Gzip for api  Handle Cors (Coss-Origin Resource Sharing)  Allow overriding HTTP method (X-HTTP-Method- Override)
  • 27. REFERENCE 27  http://www.vinaysahni.com/best-practices-for-a- pragmatic-restful-api  https://laravel.com/docs/5.3/controllers  http://blog.mwaysolutions.com/2014/06/05/10- best-practices-for-better-restful-api/  https://github.com/FriendsOfCake/crud/issues/337  https://saipraveenblog.wordpress.com/2014/09/29/ rest-api-best-practices/
  • 29. 2929

Hinweis der Redaktion

  1. https://www.drupal.org/project/restful_search_api
  2. https://github.com/FriendsOfCake/crud/issues/337
  3. https://github.com/FriendsOfCake/crud/issues/337