Zend_Controller_Router_Route [英] Zend_Controller_Router_Route
问题描述
我正在尝试制作一个可以响应此结构的路由器:
I'm trying to make a Router that can respond to this structure:
module/controller/action/id
和 module/controller/action/page
唯一的区别是id"或page".我正在使用此代码:
The only difference is is 'id' or 'page'. I'm using this code:
$routeAdmin = new Zend_Controller_Router_Route(
'administrador/:controller/:action/:id/:pg',
array(
'module' => 'administrador',
'controller' => 'index',
'action' => 'index',
'id' => 0,
'pg' => 1
),
array(
'id' => '\d+',
'pg' => '\d+'
)
);
$router->addRoute('administrador', $routeAdmin);
问题是在某些情况下我想要:
The problem is that in some situations i want:
'http://www.domain.cl/administrador/productos/2' =>(module=>administrador, controller=>productos,page=>2)
但路由器 'administrador' 结果在 'http://www.domain.cl/administrador/productos/index/0/2' (module=>administror, controller=>productos,action=>index,id=>0,page=>2)
'http://www.domain.cl/administrador/productos/2' => (module=>administrador, controller=>productos,page=>2)
but with the router 'administrador' result in 'http://www.domain.cl/administrador/productos/index/0/2' (module=>administrador, controller=>productos,action=>index,id=>0,page=>2)
对于这种情况下它是如何工作的,我感到非常困惑.我试图制作两个路由器,其中第一个只有id"参数和另一个页面"参数.并从 url helper 使用它:
I'm very confused about how it works for cases like this. I tried to make two router where the first only have 'id' param and the other 'page' param. And from url helper use it like:
$this->url(array('module' => 'administrador', 'controller' => 'productos', 'action' => 'index', 'id' => 0), 'administradorId');
$this->url(array('module' => 'administrador', 'controller' => 'productos', 'action' => 'index', 'page' => 1), 'administradorPg');
但是当我使用路由器时总是选择添加到路由器的最后一个 ($router->addRoute('routerIdentifier', $route);
)
But when I used the routers always select the last one added to the router ($router->addRoute('routerIdentifier', $route);
)
谢谢
推荐答案
我遇到了类似的问题,我通过定义一个这样的路径来解决这个问题
I have had a similar issue and I got around this by defining just one route like this
$routeAdmin = new Zend_Controller_Router_Route(
'administrador/:controller/:action/:id/:pg',
array(
'module' => 'administrador',
'controller' => 'index',
'action' => 'index',
'id' => 0
),
array(
'id' => '\d+'
)
);
$router->addRoute('administrador', $routeAdmin);
在您的 actions
中,您需要获取 id
并在 id
可能所在的地方检查它,例如如果您在 /administrador/events/view/1
中,然后您可以查看事件表,或者如果您在 /administrador/pages/view/1
中,则您可以查看一个页面.
In your actions
you'd then need to get the id
and check for it somewhere where that id
might be, for example if you were in /administrador/events/view/1
then you might look in the events table or if you were in /administrador/pages/view/1
then you would look for a page.
但是当 id
可以是给定控制器和动作中的事件或页面时,问题才真正开始.解决此问题的唯一真正方法是明确设置您使用的 id
的类型,例如
But the problems really start when the id
could be either an event or a page in a given controller and action. The only real way around this is explicitly set the type of id
your using for example
/administrador/events/view/index/id/1
或
/administrador/pages/view/index/page/1
如果你想删除索引部分,那么设置路由
If you want to remove the index part then set up routes like
$routeAdmin = new Zend_Controller_Router_Route(
// Remove the action from here and explicitly set the controller
'administrador/pages/:pg',
array(
'module' => 'administrador',
'controller' => 'pages',
// Then set the default action here
'action' => 'index',
'pg' => 0
),
array(
'pg' => '\d+'
)
);
$router->addRoute('administradorpages', $routeAdmin);
您的要求基本上会导致大量猜测工作,因此有产生意外结果的风险.
What your asking for basically results in a lot of guess work and therefore risk of producing unexpected results.
这篇关于Zend_Controller_Router_Route的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!