如何使用 Zend_Controlle 链接多个路由

发布于 2024-12-13 13:27:29 字数 1461 浏览 5 评论 0原文

我的问题是如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇妻 2024-12-20 13:27:29

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

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

$route1 = new Zend_Controller_Router_Route(
    ':year/:month/:day',
    array(
        'controller' => 'news',
        'action'     => 'year',
        'year' => '0',
        'month' => '0',
        '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.

$route1 = new Zend_Controller_Router_Route(
    ':year/:month/:day',
    array(
        'controller' => 'news',
        'action'     => 'year',
        'year' => '0',
        'month' => '0',
        'day' => '0'
    )
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文