0

In my admin module, in the index controller i have multiple filtering options. The filtering is pretty simple done, based on the parameters, ie:

http://www.site.com/admin/pages/by/date_added/order/asc  
-> This orders the pages by "date_added" ascending

http://www.site.com/admin/pages/status/published
-> This shows only the pages with status "published"

http://www.site.com/admin/pages/category/cars
-> This show only the pages under the "cars" category.

Then in my application bootstrap I have built the route like this:

$route = new Zend_Controller_Router_Route_Regex("^admin/pages/status/([a-z,_]+)$", 
    array(
       "module" => "admin",
       "controller" => "pages",
       "action" => "index"
    ),
    array(
       1 => 'by',
       2 => 'order',
    )
);
$router->addRoute("admin/pages/order", $route); 

The thing is that i don't know how to combine all these parameters but also to make them optional. For example i wan't to have links like this:

http://www.site.com/admin/pages/by/date_added/order/asc/category/cars

or...

http://www.site.com/admin/pages/category/cars/status/published
Tudor Ravoiu
  • 2,130
  • 8
  • 35
  • 56
  • Any reason you're using a regex route rather than `Zend_Controller_Router_Route`, which supports this out of the box? – Tim Fountain Oct 08 '13 at 12:42
  • @TimFountain no, no reason. i actually am looking over http://framework.zend.com/manual/1.12/en/zend.controller.router.html exactly at the Zend_Controller_Router_Route example, but i can't figure it out. – Tudor Ravoiu Oct 08 '13 at 12:44

1 Answers1

1

Try this:

$route = new Zend_Controller_Router_Route("admin/pages/*", 
    array(
       "module" => "admin",
       "controller" => "pages",
       "action" => "index"
    )
);
$router->addRoute("admin/pages/order", $route); 

The * gets it to match variables as key/value pairs. So your example URL of http://www.example.com/admin/pages/by/date_added/order/asc/category/cars should match, and would have the route params by, order and category with the values from the URL. (Accessible from your controller via. $this->_getParam('order') an so on.

Tim Fountain
  • 33,093
  • 5
  • 41
  • 69
  • yes, this works thank you. i managed in the end something similar with regex like this new Zend_Controller_Router_Route_Regex("^admin/pages/by/([a-z,_]+)/order/([a-z]+)(/status/(\w+))?$", but your solution is much simple and cleaner – Tudor Ravoiu Oct 08 '13 at 13:26