繁体   English   中英

child_routes 时出现 ZF2 错误 404

[英]ZF2 error 404 when child_routes

这是我最后的机会。 我试图寻找答案,但说真的,我看不出我做错了什么......我正在尝试在网站上设置多域。 使用文字路由时一切正常。 使用主机名路由添加其他域时,仍然可以。 但是当将 child_routes 添加到该 Hostname 路由时,父路由会发出 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),我可以访问根域

你知道我的代码有什么问题吗?

谢谢 !!!

我测试了您的代码,实际上这很奇怪,但是经过一些测试并阅读了此处的文档后,我发现了它。

这个文档帮助^ 我期待这个组件,并在文档中发现:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM