如何使用 zend router 主机名路由多个子域
我需要在 Zend 中创建路由来简单地复制当前的实时站点 url 结构,这令人遗憾的是不一致的
我想要做的是路由子域,如下所示:
www.site.com ->静态路由器
a.site.com & b.site.com ->类别控制器
c.site.com & d.site.com ->位置控制器
其余子域->用户控制器
任何人都可以指导我如何解决这个问题,谢谢。
更新:
首先感谢 Fge,投票你的答案,它有效,但我需要更多建议:
由于每个规则都有许多子域,有没有比在循环中添加规则更好的方法
foreach($子域名为$a){ $tr = 新 Zend_Controller_Router_Route_Hostname( “$a.site.com”, 大批( '模块' => '模组', '控制器' => 'ctrl', 'param_1'=>; $a )); $router->addRoute($a,$tr); 由于
- 如何将其与其他路由类型结合起来解析参数(链接?),例如 http://a.site.com/:b/:c,我想将其解析为 param_1 (a), param_2 (b), param_2 (c)
I need to create routing in Zend to simply copy the current live site url structure which is sadly inconsistent
What i want to do is to route subdomain as follow:
www.site.com -> static router
a.site.com & b.site.com -> category controller
c.site.com & d.site.com -> location controller
the rest sub domain -> user controller
could anyone guide me how to solve this, thanks.
UPDATE:
First thanks Fge, vote your answer, it works but i need some more advice:
Since i have many subdomains for each rules is there a better way than add the rules in looping
foreach($subdomains as $a){
$tr = new Zend_Controller_Router_Route_Hostname(
"$a.site.com",
array(
'module' => 'mod',
'controller' => 'ctrl',
'param_1' => $a
));
$router->addRoute($a,$tr);
}How to combine it with other routing type to parse the parameters (chained?), something like http://a.site.com/:b/:c, i want t parse it to param_1 (a), param_2 (b), param_2 (c)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(Zend_Controller_Router)
因此,您必须为所有其他路由定义路由首先是子域,然后是特定域:
更新更新的问题:
1) 这实际上取决于您想要将子域路由到的参数有多么不同。在您的示例中,您将它们全部路由到相同的模型和控制器,并添加实际的子域作为参数。这可以通过我上面发布的用户路由轻松完成。子域被设置为参数子域 (
$request->getParam("subdomain")
)。如果您希望子域成为已知控制器/模型的操作,您可以将:subdomain
替换为:action
。但是,一旦每个子域都有其他控制器/模型,我担心您必须循环它们(或使用配置文件)。对于您在问题中提供的示例,路由可能如下所示:只要您的子域中没有任何架构,就很难以通用方式路由它们。
2)这是一个示例,其中 router链开始发挥作用。外部路由是处理子域的主机名路由,内部路由将处理
:a/:b
部分。例如,这可能看起来像这样:(Zend_Controller_Router)
Thus you have to define the route for all other subdomains first, then the specific ones:
Update for the updated question:
1) This really depends on how different the parameters are you want to route a subdomain to. In your example you routed them all to the same model and controller and added the actual subdomain as a parameter. This can be done easily with the user-route i posted above. There the subdomain is set as parameter subdomain (
$request->getParam("subdomain")
). If you want the subdomains to be the action of a known controller/model you could replace:subdomain
with:action
. But as soon as you have other controllers/models for each subdomain, I'm affraid you have to loop over them (or use a config file). For the example you provided in the question, the route simply could look like this:As long as you don't have any schema in your subdomains it's very difficult to route them in a general way.
2) That's an example where router chains come into play. The outer route would be the hostname route which handles the subdomain and the inner route would handle the
:a/:b
part. This could look like this for example: