2011-11-03 49 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' 
    ) 
);