Transforming Output from the Database to the View/REST layer

Posted: 2014-06-14 23:06:36

Having watched the screen cast here by Jeffery Way and reading Phil Sturgeon's Build APIs You Won't Hate I used this approach on a recent project.

Basically, and they both cover it really well, instead of returning the data right to the ui/rest endpoint from the database I pass it through a class to transform the data formats, field name etc. This has been extremely helpful as we also work out some wording of fields that I can not do easily in this one class.

Also I can do output alterations using this Class and replace text depending on say a users permission level or maybe just to set a default image if there is none. So that user gets and Address Value Object that only shows the State.

Another good example was one server was LAMP and the other Nginx. Same db, etc but one has a setting to output a boolean as "1" and the other 1 so again I could verify and set these as needed as I built out the output. (yes I could have just Google the setting or better yet use a Vagrant box running Nginx to dev on)

Anyways just a really simple layer to add to a project.

Some good learning

  • Incremental API https://laracasts.com/series/incremental-api-development/episodes/4
  • Build APIs You Won't Hate https://leanpub.com/build-apis-you-wont-hate
  • Fractal http://fractal.thephpleague.com/simple-example/
  • Fractal Class https://github.com/thephpleague/fractal

Tags:

REST php patterns