2010-11-02 66 views
2

我想創建以下默認路由定製「REST風格的」路線:Zend Framework:如何解決這些衝突的自定義路由?

Default Route   =>  Desired Route 
============================================= 
/events/calendar    /events/calendar (stays the same, uses the default route /:controller/:action) 
/events/view/id/47    /events/47 
/events/overview/id/47   /events/47/overview 
/events/page/id/47/page-id/100 /events/47/page/100 

我想保持缺省路由,而是提高了先前的默認路由,以提高可讀性。我的路線應該如何實現?這是我到目前爲止,但他們互相沖突,所以我知道他們不正確。

$router->addRoute('eventsCalendar', new Zend_Controller_Router_Route_Static('/events/calendar', array('controller' => 'events', 'action' => 'calendar'))); 
$router->addRoute('eventPage', new Zend_Controller_Router_Route('/events/:id/:action/*', array('controller' => 'events', 'action' => 'view'))); 

回答

2

通過改變我添加路由的順序(將靜態路由移到末尾),它修復了衝突。不過,我不確定這是否是最好的方法。

$router->addRoute('eventPage', new Zend_Controller_Router_Route('/events/:id/:action/*', array('controller' => 'events', 'action' => 'view'))); 
$router->addRoute('staticEventPage', new Zend_Controller_Router_Route('/events/:id/page/:page-id', array('controller' => 'events', 'action' => 'static-page'))); 
$router->addRoute('eventsCalendar', new Zend_Controller_Router_Route_Static('/events/calendar', array('controller' => 'events', 'action' => 'calendar'))); 
+2

這是正確的做法。 ZF以相反的順序匹配路線,所以你希望你的最具體的路線最後。 – 2010-11-02 21:06:56

+0

+1當我進入自定義路線時,我會稍後在我的項目中檢查這一點。 – rvdavid 2010-11-02 22:47:30