简体   繁体   中英

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 :

'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

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 '/' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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