Zend 正则表达式路由 >跟踪api版本 [英] Zend Regex Route > Track the api version
问题描述
我正在使用 zend 构建一个 Web 服务,并且我正在使用模块来分隔我的 api 版本.例如:applications/modules/v1/controllers"、applications/modules/v2/controllers"具有不同的操作和功能集.
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.
我已将v1"作为application.ini"文件中的默认模块:
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);
假设,如果这是我的网址:http://localhost/api/v1/tags.xml
Suppose, if this is my url: http://localhost/api/v1/tags.xml
那么它属于版本 1 (v1).
then it belongs to version 1 (v1).
但我不想写很多这样的路由,所以我想知道如何从正则表达式 url 跟踪版本并动态确定要使用的 api 版本(1 或 2).
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).
推荐答案
尝试使用
$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'
)
));
这篇关于Zend 正则表达式路由 >跟踪api版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!