如何使用 Zend_Controlle 链接多个路由
我的问题是如何使用 Zend_Controller_Router_Route_Chain 链接多个路由?
例如我想链接年/月/日的 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
也许我做错了什么。请帮助我解决我的问题。谢谢。
My question is how to chain multiple routes using Zend_Controller_Router_Route_Chain?
For example I want to chain 3 routes for year/month/day
but the gole is:
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
I'm trying to use this code:
$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);
When I go to example.com/2012 and example.com/2012/11/11 everything is OK
but when i visit example.com/2012/11/ application shows me year-month-day action and on page there is
Notice: Undefined index: day in P:\Zend\ZendServer\share\ZendFramework\library\Zend\Controller\Router\Route.php on line 299
Perhaps I am doing something wrong. Please, help me to solve my problem. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,出现“未定义索引”通知是因为您没有为路由器提供任何年、月、日的默认值。
解决方案的一个想法是仅使用一个路由来匹配每个请求,使用默认值,例如 0。然后,在您的控制器中,如果“day”有默认值(day==0),则显示整个月等。
Well, the "undefined index" notice shows up because you haven't given the router any default values for year, month, and day.
One idea for a solution is using only one route, to match every request, using default values, e.g. 0. Then, in your controller, if "day" has a default value (day==0), you show the entire month etc.