当child_routes时,ZF2错误404

时间:2015-11-30 13:58:18

标签: php routes zend-framework2

这是我的最后一次机会。我试着寻找答案,但说真的,我看不出我做错了什么...... 我正在尝试在网站上设置多域名。 使用文字路线时一切正常。 使用Hostname route添加其他域时,仍然可以。 但是当将child_routes添加到该主机名路由时,父路由会射出404。 这是我的module.config:

'resources' => $resources,
'router' => array(
    'routes' => array(
        'example.com' => array(
            'type' => 'Zend\Mvc\Router\Http\Hostname',
            'options' => array(
                'route' => '[:subdomain.]:domain.:tld', // domain levels from right to left
                'contraints' => array(
                    'subdomain' => 'www',
                    'domain' => 'example',
                    'tld' => 'com',
                ),
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller' => 'Index',
                    'action' => 'itdoc',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'customCatalog2' => array(
                    'type' => 'Zend\Mvc\Router\Http\Segment',
                    'options' => array(
                        'route' => '/custom-catalog',
                        'defaults' => array(
                            'action' => 'customCatalog',
                        ),
                    ),
                ),

访问http://example.com时,我收到的是404。 但是儿童路线工作正常(http://example.com/custom-catalog) 但是如果我注释掉child_routes(和may_terminate),我可以访问根域

您对我的代码有什么问题有任何疑问吗?

谢谢!!!

1 个答案:

答案 0 :(得分:1)

我测试你的代码,事实上这很奇怪,但经过一些测试和阅读后,这里的文档就是我发现的。

This documentaiton help ^ 我期待着这个组件,并在文档中找到了这个:

'packages.zendframework.com' => array(
            'type' => 'Zend\Mvc\Router\Http\Hostname',
            'options' => array(
                'route' => ':4th.[:3rd.]:2nd.:1st', // domain levels from right to left
                'contraints' => array(
                    '4th' => 'packages',
                    '3rd' => '.*?', // optional 3rd level domain such as .ci, .dev or .test
                    '2nd' => 'zendframework',
                    '1st' => 'com',
                ),
                // Purposely omit default controller and action
                // to let the child routes control the route match
            ),
            // child route controllers may span multiple modules as desired
            'child_routes' => array(
                'index' => array(
                    'type' => 'Zend\Mvc\Router\Http\Literal',
                    'options' => array(
                        'route' => '/',
                        'defaults' => array(
                            'controller' => 'Package\Controller\Index',
                            'action' = > 'index',
                        ),
                    ),
                    'may_terminate' => true,
                ),
            ),
        ),

正如您所看到的,他们有子路线,但宣布的第一条路线是与此模式匹配的路线:'/',这是您必须声明的路线等于下面的代码:

'defaults' => array(
                '__NAMESPACE__' => 'Application\Controller',
                'controller' => 'Index',
                'action' => 'itdoc',
            ),

之后,您的主要路线是正确的,您可以继续其他子路线,只是主机名不是实际的主路线'/'

相关问题