2013-03-14 114 views
0

在我的网站上,我需要以这种方式包含页面的URL:/category/page4,因此传递给控制器​​的参数是单词“page”之后的数字。我设法得到的URL以下列方式:使用此代码/category/page/4(一个额外的斜杠):使用Zend_Controller_Router_Route解析URL中的参数

$router->addRoute('categoryPages', new Zend_Controller_Router_Route(
    '/category/page/:page', 
    array(
     'controller' => 'caegory', 
     'action' => 'index' 
    ), 
    array(
     'page' => '\d+' 
    ) 
)); 

我希望有网址,像/category/page4与此代码的以下修改:

// Simply removed one slash 
'/category/page:page' 

但是,此代码不会创建我需要的路由。我的错误是什么?

回答

1

你不能用Zend_Controller_Router_Route来做到这一点,命名变量必须与路径的url分隔符(斜杠)或开始/结束时相邻。

相反,你可以做到这一点作为一个正则表达式路线:

$router->addRoute('categoryPages', new Zend_Controller_Router_Route_Regex(
    'category/page(\d+)', 
    array(
     'controller' => 'caegory', 
     'action' => 'index' 
    ), 
    array(
     1 => 'page' 
    ), 
    'category/page%s' 
)); 
+0

非常感谢,这就是我一直在寻找。 – gvlasov 2013-03-14 14:00:33