Zend Regex Route > Track the api version

假装没事ソ 提交于 2019-12-23 03:39:15

问题


i am building a web service with zend and i am using modules to separate my api versions. Ex: "applications/modules/v1/controllers", "applications/modules/v2/controllers" have different set of actions and functionality.

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

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

I have written the following in my bootstrap file:

$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 dont want to write many routes like this one, so i want to know how can i track the version from the regex url and dynamically determine the api version to be used (1 or 2).


回答1:


try to use

$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'
                                            )
        ));



回答2:


Try this:

$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 param, and should therefor automatically route to the right module. No need to use a plug-in with the preDispatch method anymore.




回答3:


So far, i 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 a front controller and in preDispatch method, i am setting the module name based on the value i get in the "version" parameter value, like

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

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



来源:https://stackoverflow.com/questions/5167757/zend-regex-route-track-the-api-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!