在 Zend Framework 中使用 Zend_Controller_Router_Route_Regex 将多个 URL 与参数匹配 [英] Matching Multiple URLs with parameters using Zend_Controller_Router_Route_Regex in Zend Framework
问题描述
我正在使用 Zend 开发一个 Rest 控制器,但我对 url 到路由器的映射感到困惑.
基本上我读过Zend 路由器,我无法规划我的网址以满足上述路由.
这些是我应该映射到路由器的一些网址.
http://localhost/api/v1/tags.xml?abc=true(参数:abc=true)
http://localhost/api/v1/tags/123456.xml(参数:123456.xml)
http://localhost/api/v1/tags/123456/pings.xml(参数:123456,pings.xml)
http://localhost/api/v1/tags/123456/pings.xml?a=1&b=2(参数:123456,pings.xml,a=1,b=2)
http://localhost/api/v1/tags/123456/pings/count.xml(参数:123456,pings,count.xml)
我计划这样,对于 url 模式 1 到 3,标签"应该是控制器,而对于 url 模式 4 到 6,pings"应该是控制器.
现在我不确定如何配置路由器才能使上述方案起作用.请注意,我无法更改这些网址.我可以为好的答案提供 100 分的声誉分数.
前两个 URL 可以合并为一个路由器.
$r = new Zend_Controller_Router_Route_Regex('api/v1/tags.xml',数组('控制器' => '标签','动作' => '索引'));$router->addRoute('route1', $r);
要区分前两条路由,请检查标签控制器中是否存在 abc 参数.在您的标签控制器中添加以下内容,索引操作.
if($this->_getParam('abc') == "true"){//路线2} 别的 {//路线 1}
同理,4、5 路也可以合二为一.
我已经解释了路线 6.对于路线 3,您可以使用相同的逻辑.
$r = new Zend_Controller_Router_Route_Regex('api/v1/tags/(.*)/pings/(.*)',数组('控制器' => 'ping','动作' => '索引'),数组(1 => 'param1',2=>'param2'));$router->addRoute('route6', $r);
然后可以在 pings 控制器中访问参数,如下所示.
$this->_getParam('param1') 和 $this->_getParam('param2')
对于 5 号公路:
$r = new Zend_Controller_Router_Route_Regex('api/v1/tags/(.*)/pings.xml',数组('控制器' => 'ping','动作' => '索引'),数组(1 => 'param1'));$router->addRoute('route5', $r);
参数(URL 之后的一部分)不会在路由器中处理.默认情况下,它们将传递给您的控制器.
要获取在您的 URL 中传递的特定参数值,请在您的控制器中使用以下内容.
$this->_getParam('a');
逻辑是在你的路由中使用 (.*) 并为它们分配一个参数名称并在你的控制器中访问它们
i am developing a Rest Controller with Zend and i am confused with the mapping of urls to the Router.
Basically i read about Zend Router and i could not plan my urls in order to satisfy the mentioned routes.
These are some of my urls that should be mapped to Routers.
http://localhost/api/v1/tags.xml?abc=true (param: abc=true)
http://localhost/api/v1/tags/123456.xml (param: 123456.xml)
http://localhost/api/v1/tags/123456/pings.xml (params: 123456, pings.xml)
http://localhost/api/v1/tags/123456/pings.xml?a=1&b=2 (params: 123456, pings.xml, a=1, b=2)
http://localhost/api/v1/tags/123456/pings/count.xml (params: 123456, pings, count.xml)
I am planning such that for the url patterns 1 to 3, "tags" should be the controller and for the url patterns 4 to 6, "pings" should be the controller.
Now i am unsure about how to configure the routers such that the above scenarios will work. Note that i cannot change these urls. I can offer 100 of my reputation score to the good answer.
First two URLs can be combined to one router.
$r = new Zend_Controller_Router_Route_Regex('api/v1/tags.xml',
array('controller' => 'tags', 'action' => 'index'));
$router->addRoute('route1', $r);
To differentiate the first two routes, check for the presence of the abc parameter in your tags controller. Add the following in your tags controller, index action.
if($this->_getParam('abc') == "true")
{
//route 2
} else {
// route 1
}
Similarly, routes 4 and 5 can be combined into one route.
I have explained for Route 6. For route 3, you can use the same logic.
$r = new Zend_Controller_Router_Route_Regex('api/v1/tags/(.*)/pings/(.*)',
array('controller' => 'pings', 'action' => 'index'),
array(1 => 'param1',2=>'param2')
);
$router->addRoute('route6', $r);
The parameters can then accessed like the following in pings controller.
$this->_getParam('param1') and $this->_getParam('param2')
For Route 5 :
$r = new Zend_Controller_Router_Route_Regex('api/v1/tags/(.*)/pings.xml',
array('controller' => 'pings', 'action' => 'index'),
array(1 => 'param1')
);
$router->addRoute('route5', $r);
The parameters (part of the URL after ?) will not be handled in the Router. By default, they will be passed to your controller.
To get a specifc parameter value passed in your URL, use the following in your controller.
$this->_getParam('a');
The logic is use (.*) in your route and assign them a parameter name and access them in your controller
这篇关于在 Zend Framework 中使用 Zend_Controller_Router_Route_Regex 将多个 URL 与参数匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!