Zend_Controller_Router_Route
我正在尝试制作一个可以响应此结构的路由器:
module/controller/action/id
和 module/controller/action/page
唯一的区别是' id”或“页面”。我正在使用这段代码:
$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);
问题是在某些情况下我想要:
'http://www.domain.cl/administrador/productos/2' => (module=>administrador,controller=>productos,page=>2)
但使用路由器“administrador”会导致 'http://www.domain.cl/administrador/productos/ index/0/2' (module=>administrador,controller=>productos,action=>index,id=>0,page=>2)
我对它的工作原理感到非常困惑对于这样的情况。我尝试制作两个路由器,其中第一个仅具有“id”参数,另一个具有“page”参数。从 url helper 中使用它,如下所示:
$this->url(array('module' => 'administrador', 'controller' => 'productos', 'action' => ' index', 'id' => 0), 'administradorId');
$this->url(array( '模块' => '管理员' => '产品', '操作' => '索引', '页面' => 1), '管理员Pg');
但是当我使用路由器时,总是选择最后一个添加到路由器的路由器 ($router->addRoute('routerIdentifier', $route);
)
谢谢
I'm trying to make a Router that can respond to this structure:
module/controller/action/id
and module/controller/action/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)
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)
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');
But when I used the routers always select the last one added to the router ($router->addRoute('routerIdentifier', $route);
)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了类似的问题,我通过仅定义一条这样的路线来解决这个问题,
然后在您的
actions
中,您需要获取id
并在某个位置检查它id
可能是,例如,如果您位于/administrador/events/view/1
中,那么您可能会查看事件表,或者如果您位于/ administrador/pages/view/1
然后你会寻找一个页面。但当 id 可能是给定控制器和操作中的事件或页面时,问题就真正开始了。解决此问题的唯一真正方法是显式设置您使用的 id 类型,例如
,或者
如果您想删除索引部分,则设置路由,例如
您要求的内容基本上会导致大量猜测工作因此存在产生意外结果的风险。
I have had a similar issue and I got around this by defining just one route like this
In your
actions
you'd then need to get theid
and check for it somewhere where thatid
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.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 ofid
your using for exampleor
If you want to remove the index part then set up routes like
What your asking for basically results in a lot of guess work and therefore risk of producing unexpected results.
看看动态段< /a> 在路线中。这并不完全是您想要的,但它可能对您的情况有帮助。
Have a look at dynamic segments in routes. It is not exactly what you want, but it might be helpful in your case.