際際滷

際際滷Share a Scribd company logo
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 doesnt 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

More Related Content

What's hot (16)

Django Framework and Application Structure
Django Framework and Application StructureDjango Framework and Application Structure
Django Framework and Application Structure
SEONGTAEK 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 debate
Restlet
Html 5 in a big nutshell
Html 5 in a big nutshellHtml 5 in a big nutshell
Html 5 in a big nutshell
Lennart 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 cart
Self
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
Mir Nazim
Escape from the automation hell
Escape from the automation hellEscape from the automation hell
Escape from the automation hell
Nikita 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 codemotion
javier ramirez
Mastering the Oracle Data Pump API
Mastering the Oracle Data Pump APIMastering the Oracle Data Pump API
Mastering the Oracle Data Pump API
Enkitec
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 Presentation
Shrinath Shenoy
Reliable acceptance testing
Reliable acceptance testingReliable acceptance testing
Reliable acceptance testing
Dagfinn Reiers淡l
際際滷 Sahre
際際滷 Sahre際際滷 Sahre
際際滷 Sahre
karthikadevi123
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
Harry Potter
Childthemes ottawa-word camp-1919
Childthemes ottawa-word camp-1919Childthemes ottawa-word camp-1919
Childthemes ottawa-word camp-1919
Paul 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 Joomla
vdrover
Php frameworks
Php frameworksPhp frameworks
Php frameworks
Anil Kumar Panigrahi
Django Framework and Application Structure
Django Framework and Application StructureDjango Framework and Application Structure
Django Framework and Application Structure
SEONGTAEK 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 debate
Restlet
Html 5 in a big nutshell
Html 5 in a big nutshellHtml 5 in a big nutshell
Html 5 in a big nutshell
Lennart 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 cart
Self
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
Mir Nazim
Escape from the automation hell
Escape from the automation hellEscape from the automation hell
Escape from the automation hell
Nikita 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 codemotion
javier ramirez
Mastering the Oracle Data Pump API
Mastering the Oracle Data Pump APIMastering the Oracle Data Pump API
Mastering the Oracle Data Pump API
Enkitec
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 Presentation
Shrinath Shenoy
Reliable acceptance testing
Reliable acceptance testingReliable acceptance testing
Reliable acceptance testing
Dagfinn Reiers淡l
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
Harry Potter
Childthemes ottawa-word camp-1919
Childthemes ottawa-word camp-1919Childthemes ottawa-word camp-1919
Childthemes ottawa-word camp-1919
Paul 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 Joomla
vdrover

Similar to 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...
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 App
Gabriel Lucaciu
Android networking-2
Android networking-2Android networking-2
Android networking-2
Aravindharamanan S
[SharePoint Korea Conference 2013 / 螳蟲] Sharepoint る誤蟆 螳覦蠍
[SharePoint Korea Conference 2013 / 螳蟲] Sharepoint る誤蟆 螳覦蠍[SharePoint Korea Conference 2013 / 螳蟲] Sharepoint る誤蟆 螳覦蠍
[SharePoint Korea Conference 2013 / 螳蟲] Sharepoint る誤蟆 螳覦蠍
lanslote
Restful design at work v2.0
Restful design at work v2.0Restful design at work v2.0
Restful design at work v2.0
Boulder Java User's Group
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
Ho Chi Minh City Software Testing Club
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
Chi Lang Le Vu Tran
Rest API Design Rules
Rest API Design RulesRest API Design Rules
Rest API Design Rules
Mohammed Fazuluddin
SharePoint 2010 Application Development Overview
SharePoint 2010 Application Development OverviewSharePoint 2010 Application Development Overview
SharePoint 2010 Application Development Overview
Rob Windsor
En story of cakephp2.0
En story of cakephp2.0En story of cakephp2.0
En story of cakephp2.0
Hiroki 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 2010
Jeremy Thake
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
07.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 2010
Jeremy 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 SharePoint
Mark Rackley
03 form-data
03 form-data03 form-data
03 form-data
snopteck
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
Eric Shupps
API Design Tour: Dell
API Design Tour: DellAPI Design Tour: Dell
API Design Tour: Dell
Apigee | Google Cloud
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
Joel 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
Luc Bors
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 App
Gabriel 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.js
Chi Lang Le Vu Tran
SharePoint 2010 Application Development Overview
SharePoint 2010 Application Development OverviewSharePoint 2010 Application Development Overview
SharePoint 2010 Application Development Overview
Rob Windsor
En story of cakephp2.0
En story of cakephp2.0En story of cakephp2.0
En story of cakephp2.0
Hiroki 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 2010
Jeremy Thake
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
07.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 2010
Jeremy 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 SharePoint
Mark Rackley
03 form-data
03 form-data03 form-data
03 form-data
snopteck
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
Eric 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 Oleson
Joel 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
Luc Bors

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 doesnt 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

Editor's Notes

  • #13: https://www.drupal.org/project/restful_search_api
  • #24: https://github.com/FriendsOfCake/crud/issues/337
  • #25: https://github.com/FriendsOfCake/crud/issues/337