Zend控制器操作中的可选参数

时间:2013-10-08 12:20:20

标签: zend-framework routes zend-route

在我的管理模块中,在索引控制器中我有多个过滤选项。基于参数,过滤非常简单,即:

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.

然后在我的应用程序引导程序中,我构建了这样的路径:

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

问题是我不知道如何组合所有这些参数,但也使它们可选。例如,我不想有这样的链接:

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

...或

http://www.site.com/admin/pages/category/cars/status/published

1 个答案:

答案 0 :(得分:1)

试试这个:

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

*将变量与key/value对匹配。因此,http://www.example.com/admin/pages/by/date_added/order/asc/category/cars的示例网址应该匹配,并且路由参数byordercategory包含来自网址的值。 (可通过。$this->_getParam('order')从您的控制器访问。