简体   繁体   English

child_routes 时出现 ZF2 错误 404

[英]ZF2 error 404 when child_routes

This is my last chance.这是我最后的机会。 I tried loooking for answers, but seriously, I can't see what I'm doing wrong... I'm trying to set up a multi-domain on a website.我试图寻找答案,但说真的,我看不出我做错了什么......我正在尝试在网站上设置多域。 Everything is working fine when using literal routes.使用文字路由时一切正常。 When adding the other domain with Hostname route, still OK.使用主机名路由添加其他域时,仍然可以。 But when adding child_routes to that Hostname route, the parent route shoots a 404. Here's my module.config :但是当将 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',
                        ),
                    ),
                ),

When accessing http://example.com , I'm getting a 404. But the child route works fine ( http://example.com/custom-catalog ) But if I comment out the child_routes (and may_terminate), I can access the root domain访问http://example.com 时,我收到 404。但是子路由工作正常( http://example.com/custom-catalog )但是如果我注释掉 child_routes(和 may_terminate),我可以访问根域

Do you have any clue of what is wrong with my code ?你知道我的代码有什么问题吗?

Thanks !!!谢谢 !!!

I test your code and in fact this is weird, but after some test and read the docs here is what i Found.我测试了您的代码,实际上这很奇怪,但是经过一些测试并阅读了此处的文档后,我发现了它。

This documentaiton help ^ I looked forward to this component and found in the docs this : 这个文档帮助^ 我期待这个组件,并在文档中发现:

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

As you can see, they have child route but the first route declared is the route match this pattern : '/' , this is your main route you have to declare this equal to your code below :如您所见,他们有子路线,但声明的第一条路线是与此模式匹配的路线: '/' ,这是您的主要路线,您必须将其声明为下面的代码:

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

After that, your main route is correct and you can continue with other child route, just the hostname is not the actual main route '/' .之后,您的主路由是正确的,您可以继续使用其他子路由,只是主机名不是实际的主路由'/'

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

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