如何使用zend路由器主机名路由多个子域

Kom*_*ang 5 routing zend-framework zend-route

我需要在Zend中创建路由来简单地复制当前的实时站点url结构,这种结构很不一致

我想要做的是路由子域如下:

www.site.com - >静态路由器

a.site.com&b.site.com - >类别控制器

c.site.com和d.site.com - >位置控制器

其余子域 - >用户控制器

谁能指导我如何解决这个问题,谢谢.

更新:

首先感谢Fge,投票给你答案,它有效,但我还需要一些建议:

  1. 由于每个规则都有许多子域,因此有一种比在循环中添加规则更好的方法

    foreach($ subdomains as $ a){$ tr = new Zend_Controller_Router_Route_Hostname("$ a.site.com",array('module'=>'mod','controller'=>'ctrl','param_1'=> $一个 )); $路由器 - > addRoute($一个,$ TR); }

  2. 如何将它与其他路由类型结合起来解析参数(链接?),比如http://a.site.com/:b/:c,我想把它解析为param_1(a),param_2(b) ,param_2(c)

Fge*_*Fge 6

注意:反向匹配
路由以相反的顺序匹配,因此请确保首先定义最通用的路由.

(Zend_Controller_Router)

因此,您必须首先为所有其他子域定义路由,然后是特定的路径:

$user = new Zend_Controller_Router_Route_Hostname(
    ':subdomain.site.com',
    array(
        'controller' => 'user'
    )
);
$location1 = new Zend_Controller_Router_Route_Hostname(
    'c.site.com',
    array(
        'controller' => 'location'
    )
);
$location1 = new Zend_Controller_Router_Route_Hostname(
    'd.site.com',
    array(
        'controller' => 'location'
    )
);
// other definitions with known subdomain
$router->addRoute($user);   // most general one added first
$router->addRoute($location1);
$router->addRoute($location2);
// add all other subdomains
Run Code Online (Sandbox Code Playgroud)

更新更新的问题:
1)这实际上取决于您想要将子域路由到的参数有多么不同.在您的示例中,您将它们全部路由到相同的模型和控制器,并将实际的子域添加为参数.这可以通过上面发布的用户路线轻松完成.子域名设置为参数subdomain($request->getParam("subdomain")).如果你想在子域是一个已知的控制器/模型,你可以取代的作用:subdomain:action.但是只要你有每个子域的其他控制器/模型,我就会害怕你必须遍历它们(或使用配置文件).对于您在问题中提供的示例,路径可能如下所示:

$user = new Zend_Controller_Router_Route_Hostname(
    ':param1.site.com',
    array(
        'controller' => 'user'
    )
);
// routes "subdomain".site.com to defaultModul/userController/indexAction with additional parameter param1 => subdomain.
Run Code Online (Sandbox Code Playgroud)

只要您的子域中没有任何架构,就很难以一般方式对它们进行路由.

2)这是路由器链发挥作用的一个例子.外部路由将是处理子域的主机名路由,内部路由将处理该:a/:b部分.这可能看起来像这样:

$user->chain(new Zend_Controller_Router_Route(':a/:b'));
Run Code Online (Sandbox Code Playgroud)