Zend framework 如何使用zend路由器主机名路由多子域
我需要在Zend中创建路由,以便简单地复制当前的live站点url结构,这是非常不一致的 我要做的是按如下方式路由子域: www.site.com->静态路由器 a、 site.com&b.site.com->category controller c、 site.com&d.site.com->location controller rest子域->用户控制器 谁能告诉我怎么解决这个问题,谢谢 更新: 首先感谢Fge,请投票表决您的答案,它是有效的,但我需要更多的建议:Zend framework 如何使用zend路由器主机名路由多子域,zend-framework,routing,zend-route,Zend Framework,Routing,Zend Route,我需要在Zend中创建路由,以便简单地复制当前的live站点url结构,这是非常不一致的 我要做的是按如下方式路由子域: www.site.com->静态路由器 a、 site.com&b.site.com->category controller c、 site.com&d.site.com->location controller rest子域->用户控制器 谁能告诉我怎么解决这个问题,谢谢 更新: 首先感谢Fge,请投票表决您的答案,它是有效的,但我需要更多的建议: 因为每个规则都有很多子
路线是 按相反顺序匹配,因此请确保 定义了最通用的路由 首先 因此,您必须首先为所有其他子域定义路由,然后为特定子域定义路由:
$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
更新问题的更新:1) 这实际上取决于要将子域路由到的参数的不同程度。在您的示例中,您将它们全部路由到相同的模型和控制器,并添加了实际子域作为参数。这可以通过我上面发布的用户路线轻松完成。在这里,子域被设置为参数子域(
$request->getParam(“子域”)
)。如果希望子域是已知控制器/模型的操作,可以将: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.
只要您的子域中没有任何模式,就很难以一般方式对它们进行路由
2) 这是一个发挥作用的例子。外部路由将是处理子域的主机名路由,而内部路由将处理:a/:b
部分。这可能看起来像这样,例如:
$user->chain(new Zend_Controller_Router_Route(':a/:b'));
谢谢,它是有效的,投票给你的答案,但请看看我的更新上面。对不起,似乎代码格式化没有工作?