Zend Regex Route> Track api version

I am creating a web service with zend and I use modules to separate my api versions. Example: "applications / modules / v1 / controllers", "applications / modules / v2 / controllers" have a different set of actions and functionality.

I made "v1" as the default module in the file "application.ini":

resources.modules = "" resources.frontController.defaultModule = "v1" resources.frontController.moduleDirectory = APPLICATION_PATH "/modules" resources.frontController.moduleControllerDirectoryName = "controllers" 

In my boot file, I wrote the following:

 $router = $front->getRouter(); $r1 = new Zend_Controller_Router_Route_Regex('api/v1/tags.xml', array('module' => 'v1', 'controller' => 'tags', 'action' => 'index')); $router->addRoute('route1', $r1); 

Suppose if this is my url: http://localhost/api/v1/tags.xml

then it belongs to version 1 (v1).

But I don’t want to write a lot of routes like this, so I want to know how I can track the version from urge regex and dynamically determine the version of api that will be used (1 or 2).

+1
php zend-framework
source share
3 answers

try using

 $r1->addRoute( 'json_request', new Zend_Controller_Router_Route_Regex( '([^-]*)/([^-]*)/([^-]*)\.xml', array( 'controller' => 'index', 'action' => 'index', 'request_type' => 'xml'), array( 1 => 'module', 2 => 'controller', 3 => 'action' ) )); 
+1
source share

Try the following:

 $r1 = new Zend_Controller_Router_Route_Regex('api/(v.*)/tags.xml', array('module' => 'v1', 'controller' => 'tags', 'action' => 'index'), array(1 => 'module') ); 

This will automatically overwrite the module parameter and, therefore, will automatically go to the right module. There is no need to use the plugin using the preDispatch method.

+1
source share

So far I have tried like this:

 $r1 = new Zend_Controller_Router_Route_Regex('api/v(.*)/tags.xml', array('module' => 'v1', 'controller' => 'tags', 'action' => 'index'), array(1 => 'version') ); $router->addRoute('route1', $r1); 

And I could get an idea from here :

So now I used the front controller and the preDispatch method, I set the module name based on the value that I get in the value of the version parameter, for example

 if($request->getParam('version') == 2 { $request->setModuleName('v2') } 

But after changing the version in the url to v2, it still goes to the controller action in the v1 module.

0
source share

All Articles