How I built web services in CakePHP

24
How I built web services in CakePHP Amsterdam 2016

Transcript of How I built web services in CakePHP

Page 1: How I built web services in CakePHP

How I built web services in CakePHP

Amsterdam2016

Page 2: How I built web services in CakePHP

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

Agenda

System architecture

Version 1

Version 2

Version 3

CakePHP 2

CakePHP 2

CakePHP 3

Page 4: How I built web services in CakePHP

System architecture

CMSWeb

Service

Client websites

TheInternet

ApiUpdatable

Driver

Plugins

Read-only

Queue

Page 5: How I built web services in CakePHP

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

The Fat ControllerVersion 1

Page 7: How I built web services in CakePHP

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

ApiAuth Plugin/api/v1

● No standards● Open GET params● No security

Version 1

Page 9: How I built web services in CakePHP

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

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

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

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

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

Crud Index Action

Page 15: How I built web services in CakePHP

Crud Index Action

Page 16: How I built web services in CakePHP

Related dataContain all the things

Version 3

Page 17: How I built web services in CakePHP

Version 3

Filtering dataMatch all the things

Page 18: How I built web services in CakePHP

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

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

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

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

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

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

Thanks!

Questions?

Please rate my talkhttps://joind.in/talk/e2065

Attend your local PHP Usergroup! http://php.ug/

http://osmihelp.org/donate