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

时间:2015-02-07 15:38:33

标签: php zend-framework routes zend-framework2

我有2个域,我想配置每个模块路由到我的域的方式。

e.g。

 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(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
)
  

domain2模块的module.config.php

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(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
)

1 个答案:

答案 0 :(得分:0)

只要看看你的代码,你已经宣布该路线“回家”3次。请记住,当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',
                    ),
                ),
            ),
        ),
    ),
),