2011-11-03 63 views
0

我的问题是如何使用Zend_Controller_Router_Route_Chain链多个路由?如何链接多个路由Zend_Controlle

比如我想链年/月/日 3条路线,但戈莱是:

 
when url is 
example.com/2011 
runs index controller, year action 

example.com/2011/11 
runs index controller, year-month action 

example.com/2011/11/10 
runs index controller, year-month-day action 

我想使用此代码:

$front = Zend_Controller_Front::getInstance(); 
$router = $front->getRouter(); 
$chain = new Zend_Controller_Router_Route_Chain(); 

$route1 = new Zend_Controller_Router_Route(
    ':year', 
    array(
     'controller' => 'news', 
     'action'  => 'year' 
    ) 
); 

$route2 = new Zend_Controller_Router_Route(
    ':month', 
    array(
     'controller' => 'news', 
     'action'  => 'year-month' 
    ) 
); 

$route3 = new Zend_Controller_Router_Route(
    ':day', 
    array(
     'controller' => 'news', 
     'action'  => 'year-month-day' 
    ) 
); 

$chain->chain($route1) 
     ->chain($route2) 
     ->chain($route3); 

$router->addRoute('chain', $chain) 
     ->addRoute('route3', $route3) 
     ->addRoute('route2', $route2) 
     ->addRoute('route1', $route1); 

当我去以example.com/2012和example.com/2012/11/11一切都好

但是当我访问example.com/2012/11/应用程序显示我年年日行动和页面上有

 
Notice: Undefined index: day in P:\Zend\ZendServer\share\ZendFramework\library\Zend\Controller\Router\Route.php on line 299 

也许我做错了什么。请帮我解决我的问题。谢谢。

回答

0

那么,“未定义索引”通知显示,因为你没有给路由器任何默认值的年,月和日。

解决方案的一个想法是仅使用一个路由来匹配每个请求,使用默认值(例如,然后,在您的控制器中,如果“日”具有默认值(日期== 0),则显示整个月份等。

$route1 = new Zend_Controller_Router_Route(
    ':year/:month/:day', 
    array(
     'controller' => 'news', 
     'action'  => 'year', 
     'year' => '0', 
     'month' => '0', 
     'day' => '0' 
    ) 
);