how i built web services in cakephp
TRANSCRIPT
![Page 1: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/1.jpg)
How I built web services in CakePHP
Amsterdam2016
![Page 2: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/2.jpg)
Introduction
PHP Developerat UK Web Media
in Weyhill, UKhttp://www.ukwm.co.uk
Cyberstalk me!https://twitter.com/YellDavidhttps://github.com/davidyell
http://careers.stackoverflow.com/davidyellhttp://uk.linkedin.com/in/davidyell
#cakephp on Freenode IRC
David Yellaka Neon1024
![Page 3: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/3.jpg)
Agenda
System architecture
Version 1
Version 2
Version 3
CakePHP 2
CakePHP 2
CakePHP 3
![Page 4: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/4.jpg)
System architecture
CMSWeb
Service
Client websites
TheInternet
ApiUpdatable
Driver
Plugins
Read-only
Queue
![Page 5: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/5.jpg)
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
![Page 6: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/6.jpg)
The Fat ControllerVersion 1
![Page 7: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/7.jpg)
Api Plugin/api/v1
● Hard to maintain● Lack of flexibility● Not DRY● No separation of
concerns● Custom views
Version 1
![Page 8: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/8.jpg)
ApiAuth Plugin/api/v1
● No standards● Open GET params● No security
Version 1
![Page 9: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/9.jpg)
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
![Page 10: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/10.jpg)
API Design choices
Pragmatic approachComplex queries but more complete responses
Allows complete configured returnFlexible filtering and sorting
Specific use-cases
Further reading, http://www.bravo-kernel.com/tags/api/
![Page 11: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/11.jpg)
Version 3
Routing
Request
Response
api prefixv3 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
![Page 12: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/12.jpg)
Version with prefixes
src/Controller/Api/V3/PackagesController.php
src/Controller/Api/V4/PackagesController.php
/api/v3/packages.json
/api/v4/packages.json
![Page 13: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/13.jpg)
Crud pluginfriendsofcake/crud
Event driven scaffoldingBe lazy
Do less work??
Profit
http://www.bravo-kernel.com/2015/04/how-to-build-a-cakephp-3-rest-api-in-minutes/
![Page 14: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/14.jpg)
Crud Index Action
![Page 15: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/15.jpg)
Crud Index Action
![Page 16: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/16.jpg)
Related dataContain all the things
Version 3
![Page 17: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/17.jpg)
Version 3
Filtering dataMatch all the things
![Page 18: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/18.jpg)
Version 3
Example queriesGet 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!
![Page 19: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/19.jpg)
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
![Page 20: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/20.jpg)
Lessons learned
Don't● Single controller● Filter in controller
Do● Routing prefixes● Crud plugin● Authentication● Create separation● Integration testing
![Page 21: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/21.jpg)
Toolset
friendsofcake/crud
friendsofcake/search
admad/cakephp-jwt-auth
muffin/webservice
league/fractal
https://www.getpostman.com/
![Page 22: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/22.jpg)
Consuming the APICake 2
Custom data source
http://book.cakephp.org/2.0/en/models/datasources.html
![Page 23: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/23.jpg)
Muffin/WebService Cake 3 driver
http://jedistirfry.co.uk/blog/2015-09/connecting-to-a-web-service/
Consuming the APICake 3
![Page 24: How I built web services in CakePHP](https://reader034.vdocuments.us/reader034/viewer/2022050614/58794c891a28abb1418b50e9/html5/thumbnails/24.jpg)
Thanks!
Questions?
Please rate my talkhttps://joind.in/talk/e2065
Attend your local PHP Usergroup! http://php.ug/
http://osmihelp.org/donate