0

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

例如,我想为年/月/日链接 3 条路线,但 gole 是:

当 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/ 应用程序向我显示年月日操作时,页面上有

注意:未定义索引:第 299 行 P:\Zend\ZendServer\share\ZendFramework\library\Zend\Controller\Router\Route.php 中的日期

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

4

1 回答 1

0

好吧,“未定义索引”通知出现了,因为您没有为路由器提供任何年、月和日的默认值。

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

$route1 = new Zend_Controller_Router_Route(
    ':year/:month/:day',
    array(
        'controller' => 'news',
        'action'     => 'year',
        'year' => '0',
        'month' => '0',
        'day' => '0'
    )
);
于 2012-04-30T22:30:14.280 回答