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

发布于 2024-10-01 08:12:01 字数 758 浏览 2 评论 0原文

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

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

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

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

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

其余子域->用户控制器

任何人都可以指导我如何解决这个问题,谢谢。

更新:

首先感谢 Fge,投票你的答案,它有效,但我需要更多建议:

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

    foreach($子域名为$a){ $tr = 新 Zend_Controller_Router_Route_Hostname( “$a.site.com”, 大批( '模块' => '模组', '控制器' => 'ctrl', 'param_1'=>; $a )); $router->addRoute($a,$tr); 由于

  2. 如何将其与其他路由类型结合起来解析参数(链接?),例如 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:

  1. 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);
    }

  2. 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 技术交流群。

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

发布评论

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

评论(1

分分钟 2024-10-08 08:12:02

注意:反向匹配
路线有
以相反的顺序匹配,因此请确保
您最通用的路线已定义
首先。

(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

更新更新的问题:
1) 这实际上取决于您想要将子域路由到的参数有多么不同。在您的示例中,您将它们全部路由到相同的模型和控制器,并添加实际的子域作为参数。这可以通过我上面发布的用户路由轻松完成。子域被设置为参数子域 ($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.

只要您的子域中没有任何架构,就很难以通用方式路由它们。

2)这是一个示例,其中 router链开始发挥作用。外部路由是处理子域的主机名路由,内部路由将处理 :a/:b 部分。例如,这可能看起来像这样:

$user->chain(new Zend_Controller_Router_Route(':a/:b'));

Note: Reverse Matching
Routes are
matched in reverse order so make sure
your most generic routes are defined
first.

(Zend_Controller_Router)

Thus you have to define the route for all other subdomains first, then the specific ones:

$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

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:

$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.

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:

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