How I built web services in CakePHP

  • View
    1.502

  • Download
    0

  • Category

    Software

Preview:

Citation preview

How I built web services in CakePHP

Amsterdam2016

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

Agenda

System architecture

Version 1

Version 2

Version 3

CakePHP 2

CakePHP 2

CakePHP 3

System architecture

CMSWeb

Service

Client websites

TheInternet

ApiUpdatable

Driver

Plugins

Read-only

Queue

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

The Fat ControllerVersion 1

Api Plugin/api/v1

● Hard to maintain● Lack of flexibility● Not DRY● No separation of

concerns● Custom views

Version 1

ApiAuth Plugin/api/v1

● No standards● Open GET params● No security

Version 1

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

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/

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

Version with prefixes

src/Controller/Api/V3/PackagesController.php

src/Controller/Api/V4/PackagesController.php

/api/v3/packages.json

/api/v4/packages.json

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/

Crud Index Action

Crud Index Action

Related dataContain all the things

Version 3

Version 3

Filtering dataMatch all the things

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!

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

Lessons learned

Don't● Single controller● Filter in controller

Do● Routing prefixes● Crud plugin● Authentication● Create separation● Integration testing

Toolset

friendsofcake/crud

friendsofcake/search

admad/cakephp-jwt-auth

muffin/webservice

league/fractal

https://www.getpostman.com/

Consuming the APICake 2

Custom data source

http://book.cakephp.org/2.0/en/models/datasources.html

Muffin/WebService Cake 3 driver

http://jedistirfry.co.uk/blog/2015-09/connecting-to-a-web-service/

Consuming the APICake 3

Thanks!

Questions?

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

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

http://osmihelp.org/donate