Zend Framework 2 - 多个子域名引起的问题

3

我正在使用Zend Framework 2编写一个应用程序,它将从几个不同的子域运行,并且我希望每个子域都有不同的模块,以保持整洁。

我的问题是,当我添加多个子域到路由中时,它会丢失其中一个子域。

例如:这个设置是可以工作的 testbed.localhost(module/Application) a.testbed.localhost(module/A)

如果我添加一个额外的子域,它将路由所有对A的请求到Application Index Controller

例如: testbed.localhost(module/Application),a.testbed.localhost(module/A),b.testbed.localhost(module/B)

这是module/A的module.config.php文件:

    'router' => array(
    'routes' => array(
        'ads' => array(
            'type'    => 'Hostname',
            'options' => array(
                 'route'    => 'a.testbed.localhost', 
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    '__NAMESPACE__' => 'A\Controller',
                    'controller' => 'A\Controller\A',
                    'action'     => 'index',
                ),
            ),

以下是module/B中module.config.php文件中的路由:

    'router' => array(
    'routes' => array(
        'ads' => array(
            'type'    => 'Hostname',
            'options' => array(
                 'route'    => 'b.testbed.localhost', 
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    '__NAMESPACE__' => 'B\Controller',
                    'controller' => 'B\Controller\B',
                    'action'     => 'index',
                ),
            ),

现在两个module.config.php文件中的命名空间都是正确的,我注意到如果我从config/application.config.php中删除对子域a.testbed.localhost的引用,它将可以正常工作。
    <?php
    return array(
        'modules' => array(
         'Application',
         'A',
         'B', <--- A doesn't work if B is here
      ),

如果我在上面的模块数组中交换A和B,那么B将被转发到应用程序模块,而A将起作用。因此,它似乎存在多个子域的问题。有人有什么想法/遇到过同样的问题吗?


2
我认为问题可能在于你的路由名称相同。也许尝试使用a-ads和b-ads作为路由名称,然后看看会发生什么。 - Diemuzi
1
你说得对,天啊,我简直不敢相信我没有发现那个问题。昨天一直盯着它看了几个小时! - deadtoy
你能否关闭这个问题,以便它不会留在未回答的列表中? - DASPRiD
@Diemuzi,你解决了我的问题。请把你的评论写成答案。 - Ashwini Agarwal
@deadtoy,你能否关闭这个问题。 - hohner
1个回答

2

这是因为你的路由名称相同。我建议将路由名称分别设置为a-ads和b-ads,这样就可以解决你的问题。

最终配置会被合并在一起。就像一个数组,当最后一个数组被合并时,它会覆盖之前的任何内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接