Question

I'm writing a new version of an API and would like to support legacy versions by having distinct sets of controllers for each version. Within the default "app\controllers" path in Lithium, I would like to have for example "v1" and "v2" paths.

I have tried accomplishing this within the route itself by doing something like:

 Router::connect('/{:version}/{:controller}/{:action}{:args}', array(
        'controller'=> '\app\controllers\{:version}\{:controller}Controller',
    ), array());

Then I tried overriding the path in the libraries bootstrap module by doing something like:

if( preg_match('/^\/(v[0-9\.]+)/', $_SERVER['REQUEST_URI'], $match) ) {
    Libraries::paths(array(
        'controllers' => "controllers\\".$match[1].'\\{:name}Controller',
        'models' => "models\\".$match[1]."\\{:name}",
    ));
}

I spent about a half a day at work searching google and the very sparse lithium docs. I am not sure what release of Lithium we are using as I have stepped into this pre-existing code base.

Thanks for any tips you may have!

Was it helpful?

Solution

In your routes.php file, you should re-configure the Dispatcher default rules with

Dispatcher::config(array('rules' => array(
  'v1' => array('controller' => 'app\controllers\v1\{:controller}Controller')
)));

and a continuation route to match /v1/... requests

Router::connect('/v1/{:args}', array('v1' => true), array(
  'continue' => true, 'persist' => array('controller', 'v1')
));

You can easily use :version instead of a predefined version number if you need so.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top