Zend Framework 主机名路由:在忽略子域的同时进行路由 [英] Zend Framework Hostname Routes: Routing while ignoring subdomains

查看:20
本文介绍了Zend Framework 主机名路由:在忽略子域的同时进行路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想创建一个 Zend_Controller_Router_Route_Hostname 匹配以下内容:

So I want to create a Zend_Controller_Router_Route_Hostname that matches the following:

  • example.com
  • www.example.com
  • stage.example.com
  • dev.example.com
  • andrew.dev.example.com
  • joe.dev.example.com

我似乎无法弄清楚如何创建一条可以匹配所有这些东西的路线.example.com"之前可以有多个子域,因此之前的所有内容都无关紧要.这是我到目前为止所拥有的:

I can't seem to figure out how to create a route that can match all these things. There can be multiple subdomains before "example.com", so everything before it doesn't really matter. Here's what I have so far:

$hostnameRoute = new Zend_Controller_Router_Route_Hostname(':sandbox.:environment.example.com', array('controller' => 'events', 'event-id' => $eventId));

推荐答案

我认为在单个主机名路由中不可能有多个子域匹配.所以这是你必须做的:

I don't think it's possible to have multiple subomains match in a single hostname route. So here's what you have to do:

$hostnameRoutes = array(
    new Zend_Controller_Router_Route_Hostname('example.com', array('controller' => 'events', 'event-id' => $eventId)),
    new Zend_Controller_Router_Route_Hostname(':subdomain.example.com', array('controller' => 'events', 'event-id' => $eventId)),
    new Zend_Controller_Router_Route_Hostname(':sandbox.dev.example.com', array('controller' => 'events', 'event-id' => $eventId))
);

$homepageRoute =         new Zend_Controller_Router_Route_Static('',       array('action' => 'overview'));
$eventPagesRoute =       new Zend_Controller_Router_Route(':action/*',     array('action' => 'overview'));
$staticEventPagesRoute = new Zend_Controller_Router_Route('page/:page-id', array('action' => 'static-page'));

foreach ($hostnameRoutes as $i => $hostnameRoute) {
    $router->addRoute('sports_homepage' . $i,          $hostnameRoute->chain($homepageRoute));
    $router->addRoute('sports_event_pages' . $i,       $hostnameRoute->chain($eventPagesRoute));
    $router->addRoute('sports_static_event_Page' . $i, $hostnameRoute->chain($staticEventPagesRoute));
}

注意:确保你的路由名称是唯一的(注意 $i 被连接起来使它们唯一).我第一次忘记了,不知道为什么它不起作用.

Note: make sure the names of your routes are unique (note the $i being concatenated to make them unique). I forgot about that first time through and couldn't figure out why it wasn't working.

这篇关于Zend Framework 主机名路由:在忽略子域的同时进行路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆