如何将Zend Framework 2的每个模块配置为每个主机名


How to configure Zend Framework 2 each module to each Hostname?

我有两个域,我想配置这样一种方式,即每个模块都路由到我的域。

例如

 1. domain1 module = domain1.ashwin.com
 2. domain2 module = domain2.ashwin.com

我有以下代码:

domain1模块的module.config.php

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Hostname',
                'options' => array(
                    'route'    => 'domain1.ashwin.com',
                    'defaults' => array(
                        'controller' => 'Domain1'Controller'Index',
                        'action'     => 'index',
                    ),
                ),
            ),
            'home' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Domain1'Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/[:controller[/:action]]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
)

module.config.php用于domain2模块

return array(
    'router' => array(
        'routes' => array(
            'domain2' => array(
                'type' => 'Hostname',
                'options' => array(
                    'route'    => 'domain2.ashwin.com',
                    'defaults' => array(
                        'controller' => 'Domain2'Controller'Index',
                        'action'     => 'index',
                    ),
                ),
            ),
            'home' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Domain2'Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/[:controller[/:action]]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
)

只需查看您的代码,您就已经声明了3次路由"home"。请记住,当Zend时,路由实际上被合并到了一个大数组中启动您的模块。

如果我们以domain1的module.config.php为例你应该做这样的

'routes' => array(
    'domain1' => array(
        'type' => 'Hostname',
        'options' => array(
            'route'    => 'domain1.ashwin.com',
            'defaults' => array(
                'controller' => 'Domain1'Controller'Index',
                'action'     => 'index',
            ),
        ),
        'may_terminate' => true,
        'child_routes' => array(
            'default' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route'    => '/[:controller[/:action]]',
                    'constraints' => array(
                        'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                        '__NAMESPACE__' => 'Domain1'Controller',
                    ),
                ),
            ),
        ),
    ),
),