Versioning a PHP Lithium API -


goal

i've been tasked versioning pretty large php lithium api.

the end result i'm looking use namespacing separate versions.

for example, lithium route looked this:

router::connect('/{:controller}/{:action}/{:id:\d+}'); 

could have following url, map following php call:

http://www.fungames.com/game/view/2 app\controllers\game::view($gameid) 

but create following mapping:

router::connect('/{:version}/{:controller}/{:action}/{:id:\d+}'); 

such following 2 calls made:

http://www.fungames.com/v1/game/view/2 app\controllers\v1\game::view($gameid)  http://www.fungames.com/v2/game/view/2 app\controllers\v2\game::view($gameid) 

problem

unfortunately, lithium documentation doesn't make mention of api versioning. there's brief mention here example of continuation routes. approach required making if statements in controllers version api, , consider poor approach.


tldr

what best way achieved namespaced api versioning when using php lithium framework?

api versioning broad topic, , while li3 gives necessary tools implement approach choose, actual implementation you. having said that, routing layer sophisticated , very, far. see this gist on routing configuration examples.

combining request::get() allows match on request parameters including headers (you can pass get()-compatible values $params in router::connect()see examples here), , request::detect(), allows implement custom matching logic, can send configuration of values down controller/dispatch layer. allows segment versions differently namespaced controllers (with fallback default), prefixed actions, or pass namespace/class path different model.

yet approach, , 1 recommend in general case, implementing set of transaction managers/mappers endpoints need versioned, , using them intermediary between controller , model, use routing switch between them.

this approach made easier if evolve api using, i.e. content types, since can choose mapper based on that. nice since version numbers discouraged rest's creator.

anyway, hope helps.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -