Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Zend Regex路线>;跟踪api版本_Php_Zend Framework - Fatal编程技术网

Php Zend Regex路线>;跟踪api版本

Php Zend Regex路线>;跟踪api版本,php,zend-framework,Php,Zend Framework,我正在使用zend构建一个web服务,并使用模块来分离api版本。例如:“应用程序/模块/v1/控制器”、“应用程序/模块/v2/控制器”具有不同的操作和功能集 我已将“v1”作为“application.ini”文件中的默认模块: 我已在引导文件中写入以下内容: $router = $front->getRouter(); $r1 = new Zend_Controller_Router_Route_Regex('api/v1/tags.xml', ar

我正在使用zend构建一个web服务,并使用模块来分离api版本。例如:“应用程序/模块/v1/控制器”、“应用程序/模块/v2/控制器”具有不同的操作和功能集

我已将“v1”作为“application.ini”文件中的默认模块:

我已在引导文件中写入以下内容:

$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);
假设,如果这是我的url:
http://localhost/api/v1/tags.xml

那么它属于版本1(v1)


但是我不想写很多这样的路由,所以我想知道如何从regex url跟踪版本并动态确定要使用的api版本(1或2)。

到目前为止,我尝试了以下方法:

$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);
我可以从以下方面得到一个想法:

所以现在我使用了一个前端控制器,在preDispatch方法中,我根据“version”参数值中的值设置模块名称,如

if($request->getParam('version') == 2 { $request->setModuleName('v2') }
但将url中的版本更改为v2后,仍然会转到v1模块中控制器的操作。

尝试以下操作:

$r1 = new Zend_Controller_Router_Route_Regex('api/(v.*)/tags.xml',
        array('module' => 'v1', 'controller' => 'tags', 'action' => 'index'),
        array(1 => 'module')
);
这将自动覆盖模块参数,因此应自动路由到正确的模块。不再需要使用带有
preDispatch
方法的插件。

尝试使用

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

很抱歉,我必须使用前端控制器插件进行某种验证,尽管如此,上面的修改对我来说并不适用。我还是去v1模块控制器。嗯,这很奇怪。我相信这应该是可行的,除非你的路线已经被另一条路线提前截获。你还定义了其他路线吗?是的,我定义了许多类似的路线。。。不管怎样,弗雷德尼提出的解决方案对我来说很有效。谢谢:)
$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'
                                            )
        ));