我的问题是如何使用Zend_Controller_Router_Route_Chain链接多条路由?
例如,我想链接年/月/日的3条路由但目标是:
<>之前当url为example.com/2011运行索引控制器,年份动作example.com/2011/11运行指数控制器,年-月动作example.com/2011/11/10运行指数控制器,年-月-日动作之前我正在尝试使用这个代码:
$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/时,应用程序显示了年-月-日的操作,页面上有
<>之前注意:未定义的索引:P:'Zend'ZendServer'share'ZendFramework'library'Zend'Controller'Router'Route.php中的天之前也许我做错了什么。请帮我解决我的问题。谢谢。
之所以会出现"undefined index"通知,是因为你没有给路由器指定年、月和日的默认值。
解决方案的一个想法是只使用一条路由来匹配每个请求,使用默认值,例如0。然后,在控制器中,如果"day"有一个默认值(day==0),则显示整个月份,等等。
$route1 = new Zend_Controller_Router_Route(
':year/:month/:day',
array(
'controller' => 'news',
'action' => 'year',
'year' => '0',
'month' => '0',
'day' => '0'
)
);