Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

How I built web services in CakePHP

2,483 views

Published on

A presentation about building web services in CakePHP 2 and CakePHP 3

Published in: Software
  • Be the first to comment

  • Be the first to like this

How I built web services in CakePHP

  1. 1. How I built web services in CakePHP Amsterdam 2016
  2. 2. Introduction PHP Developer at UK Web Media in Weyhill, UK http://www.ukwm.co.uk Cyberstalk me! https://twitter.com/YellDavid https://github.com/davidyell http://careers.stackoverflow.com/davidyell http://uk.linkedin.com/in/davidyell #cakephp on Freenode IRC David Yell aka Neon1024
  3. 3. Agenda System architecture Version 1 Version 2 Version 3 CakePHP 2 CakePHP 2 CakePHP 3
  4. 4. System architecture CMS Web Service Client websites The Internet ApiUpdatable Driver Plugins Read-only Queue
  5. 5. Setup /api/v1 Api ApiAuth Yey! ● Plugin architecture ● Self-contained Boo! ● Single fat controller ● Lack of separation ● Less flexible Routing Request Response Plugins Version 1 v1 prefix
  6. 6. The Fat Controller Version 1
  7. 7. Api Plugin /api/v1 ● Hard to maintain ● Lack of flexibility ● Not DRY ● No separation of concerns ● Custom views Version 1
  8. 8. ApiAuth Plugin /api/v1 ● No standards ● Open GET params ● No security Version 1
  9. 9. Setup /api/v2 Yey! ● Shares existing code ● More generic ● Extensibility ● Per endpoint customisation Boo! ● Hard to create common filtering ● Too much code in AppController ● Lacks separation Routing Request Response Application Version 2 v2 prefix AppController ProductsController
  10. 10. API Design choices Pragmatic approach Complex queries but more complete responses Allows complete configured return Flexible filtering and sorting Specific use-cases Further reading, http://www.bravo-kernel.com/tags/api/
  11. 11. Version 3 Routing Request Response api prefix v3 route ApiAppController Crud plugin Custom Crud actions Contain class Filter class Yey! ● Crud automation ● Free sorting and pagination ● Code separation ● Extensible Boo! ● Complexity Setup /api/v3 ApiQuery Behavior
  12. 12. Version with prefixes src/Controller/Api/V3/PackagesController.php src/Controller/Api/V4/PackagesController.php /api/v3/packages.json /api/v4/packages.json
  13. 13. Crud plugin friendsofcake/crud Event driven scaffolding Be lazy Do less work ?? Profit http://www.bravo-kernel.com/2015/04/how-to-build-a-cakephp-3- rest-api-in-minutes/
  14. 14. Crud Index Action
  15. 15. Crud Index Action
  16. 16. Related data Contain all the things Version 3
  17. 17. Version 3 Filtering data Match all the things
  18. 18. Version 3 Example queries Get packages with related data, where line rental is included, from provider id 4, matching an associated Table field, sorted by price, on page 2. /api/v3/packages.json ?verbose=true &filter[line_rental_included]=true &filter[provider_id]=4 &filter[Related.something]=foo &site_id=n &limit=20 &page=3 &sort=price &direction=asc Free with Crud plugin!
  19. 19. Version 3 Example queries Get a specific list of packages in a certain order. /api/v3/packages.json ?id[0]=19 &id[1]=17 &id[2]=11 &id[3]=43 &site_id=n
  20. 20. Lessons learned Don't ● Single controller ● Filter in controller Do ● Routing prefixes ● Crud plugin ● Authentication ● Create separation ● Integration testing
  21. 21. Toolset friendsofcake/crud friendsofcake/search admad/cakephp-jwt-auth muffin/webservice league/fractal https://www.getpostman.com/
  22. 22. Consuming the API Cake 2 Custom data source http://book.cakephp.org/2.0/en/models/datasources.html
  23. 23. Muffin/WebService Cake 3 driver http://jedistirfry.co.uk/blog/2015-09/connecting-to-a-web-service/ Consuming the API Cake 3
  24. 24. Thanks! Questions? Please rate my talk https://joind.in/talk/e2065 Attend your local PHP Usergroup! http://php.ug/ http://osmihelp.org/donate

×