Zend_Controller_Router_Route

我们两清 提交于 2019-12-23 23:51:52

问题


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:

  1. $this->url(array('module' => 'administrador', 'controller' => 'productos', 'action' => 'index', 'id' => 0), 'administradorId');

  2. $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


回答1:


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

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.

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

or

/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.




回答2:


Have a look at dynamic segments in routes. It is not exactly what you want, but it might be helpful in your case.



来源:https://stackoverflow.com/questions/4846726/zend-controller-router-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!