ZF2 router configuration for subdomain with child routes

一笑奈何 提交于 2019-12-11 01:38:15

问题


I need my router configuration to work as follows:

www.bmob.co.uk           -> DtCompanyData\Controller\CompanyMap   
wage.bmob.co.uk          -> DtWage\Controller\Wage  
wage.bmob.co.uk/brighton -> DtWage\Controller\WageBrighton  

After reading the documentation and many similar questions on here, I can get the routing to work either for the subdomain, or for the child routes, but not both at the same time.

When I have

// In Application/config/module.config.php:

'home' => array(
    'type' => 'hostname',
    'options' => array(
        'route' => 'www.bmob.co.uk',
        'defaults' => array(
            '__NAMESPACE__' => 'DtCompanyData\Controller',
            'controller' => 'DtCompanyData\Controller\CompanyMap',
            'action' => 'index',
        ),
    ),
),

// In DtWage/config/module.config.php:

'wage' => array(
    'type' => 'hostname',
    'options' => array(
        'route' => 'wage.bmob.co.uk',
        'defaults' => array(
            '__NAMESPACE__' => 'DtWage\Controller',
            'controller' => 'DtWage\Controller\Wage',
            'action' => 'index',
        ),
    ),
    'may_terminate' => true,
),

I get:

www.bmob.co.uk - works  
wage.bmob.co.uk - works  
wage.bmob.co.uk/brighton - goes to same page as wage.bmob.co.uk

When I have:

// In Application/config/module.config.php:

'home' => array(
    'type' => 'hostname',
    'options' => array(
        'route' => 'www.bmob.co.uk',
        'defaults' => array(
            '__NAMESPACE__' => 'DtCompanyData\Controller',
            'controller' => 'DtCompanyData\Controller\CompanyMap',
            'action' => 'index',
        ),
    ),
),

// In DtWage/config/module.config.php:

'wage' => array(
    'type' => 'hostname',
    'options' => array(
        'route' => 'wage.bmob.co.uk',
        'defaults' => array(
            '__NAMESPACE__' => 'DtWage\Controller',
            'controller' => 'DtWage\Controller\Wage',
            'action' => 'index',
        ),
    ),
    'may_terminate' => true,
    'child_routes' => array(
        'wagebrighton' => array( 
            'type' => 'literal',
            'options' => array(
                'route' => '/brighton',
                'defaults' => array(
                    '__NAMESPACE__' => 'DtWage\Controller',   // Have also tried without this line, same result
                    'controller' => 'DtWage\Controller\WageBrighton',
                    'action' => 'index',
                ),
            ),
        ),
    ),
),

I get:

www.bmob.co.uk - works  
wage.bmob.co.uk - 404 The requested URL could not be matched by routing.  
wage.bmob.co.uk/brighton - works

What am I doing wrong here? How to I get wage.bmob.co.uk and wage.bmob.co.uk/brighton to both work at the same time? Thanks.


回答1:


I would try two configurations :

'wage' => array(
    'type' => 'hostname',
    'options' => array(
        'route' => 'wage.bmob.co.uk/',
        'defaults' => array(
            '__NAMESPACE__' => 'DtWage\Controller',
            'controller' => 'DtWage\Controller\Wage',
            'action' => 'index',
        ),
    ),
    'may_terminate' => true,
    'child_routes' => array(
        'wagebrighton' => array(
            'type' => 'literal',
            'options' => array(
                'route' => '/brighton',
                'defaults' => array(
                    '__NAMESPACE__' => 'DtWage\Controller', // Have also tried without this line, same result
                    'controller' => 'DtWage\Controller\WageBrighton',
                    'action' => 'index',
                ),
            ),
        ),
    ),
),

or

'wage' => array(
    'type' => 'hostname',
    'options' => array(
        'route' => 'wage.bmob.co.uk',
        'defaults' => array(
            '__NAMESPACE__' => 'DtWage\Controller',
        ),
    ),
    'may_terminate' => false,
    'child_routes' => array(
        'wagehome' => array(
            'type' => 'literal',
            'options' => array(
                'route' => '/',
                'defaults' => array(
                    '__NAMESPACE__' => 'DtWage\Controller',
                    'controller' => 'DtWage\Controller\Wage',
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'wagebrighton' => array(
                    'type' => 'literal',
                    'options' => array(
                        'route' => '/brighton',
                        'defaults' => array(
                            '__NAMESPACE__' => 'DtWage\Controller',
                            'controller' => 'DtWage\Controller\WageBrighton',
                            'action' => 'index',
                        ),
                    ),
                ),
            ),
        ),
    ),
),

EDIT : what about this one :

'wage' => array(
    'type' => 'hostname',
    'options' => array(
        'route' => 'wage.bmob.co.uk',
        'defaults' => array(
            '__NAMESPACE__' => 'DtWage\Controller',
        ),
    ),
    'may_terminate' => false,
    'child_routes' => array(
        'wagehome' => array(
            'type' => 'literal',
            'options' => array(
                'route' => '/',
                'defaults' => array(
                    '__NAMESPACE__' => 'DtWage\Controller',
                    'controller' => 'DtWage\Controller\Wage',
                    'action' => 'index',
                ),
            ),
        ),
        'wagebrighton' => array(
            'type' => 'literal',
            'options' => array(
                'route' => '/brighton',
                'defaults' => array(
                    '__NAMESPACE__' => 'DtWage\Controller',
                    'controller' => 'DtWage\Controller\WageBrighton',
                    'action' => 'index',
                ),
            ),
        ),
    ),
),


来源:https://stackoverflow.com/questions/21933401/zf2-router-configuration-for-subdomain-with-child-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!