codeigniterversioningbackwards-compatibility

Proper custom API Versioning in CodeIgniter


I'm have built a CodeIgniter 2.0 site that provides an API. I'm now at the point where I need to create another version of the API that will not be backwards compatible with the previous version. Here's what I'm trying to do:

  1. Create versioned API controllers, something like {root}/application/controllers/api/v1.0/api.php and /v2.0/api.php, etc...
  2. Allow higher API versions to inherit from previous versions so I only have to override those methods that I want to

CodeIgniter doesn't seem to like this type of folder structure. And also, if all my API controllers have the same class name, I won't be able to subclass (i.e. "class API extends API" won't work). That said, I'd prefer to not have to do this: "class API2_0 extends API1_0", but I will if necessary.

I feel like URI Routing will be the key here (which I have limited grasp of), but if someone has already going through this thought process, I'd very much appreciate some guidance.


Solution

  • Structure you API however you normally would as far as controllers and whatnot go, then just have:

    application/controllers/api_1/foo.php
    application/controllers/api_2/foo.php
    

    Using Routes you can make that:

    /api/1/foo
    /api/2/foo