3

I'm having some troubles setting up child_routes. They don't work unless I separate them, althou the end result should be the same!:

This is what I'm trying to achieve:

'router' => array(
        'routes' => array(
            'app' => array(
                'type' => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                    'route' => '[/:info]/app',
                    'defaults' => array(
                        '__NAMESPACE__' => 'X\App',
                        'controller' => 'Index',
                        'action' => 'index',
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        'example' => array(
                            'type' => 'Zend\Mvc\Router\Http\Segment',
                            'options' => array(
                                'route' => '/example[:/data]',
                                'defaults' => array(
                                    'action' => 'example',
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),

But it only works this way:

'router' => array(
        'routes' => array(
            'app' => array(
                'type' => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                    'route' => '[/:info]/app',
                    'defaults' => array(
                        '__NAMESPACE__' => 'X\App',
                        'controller' => 'Index',
                        'action' => 'index',
                    ),                    
                ),
            ),
            'app.example' => array(
                'type' => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                    'route' => '[/:info]/app/example[/:data]',
                    'defaults' => array(
                        '__NAMESPACE__' => 'X\App',
                        'controller' => 'Index',
                        'action' => 'example',
                    ),
                ),
            ),
        ),

.. anyone knows what I might be doing wrong..?

MGP
  • 653
  • 1
  • 14
  • 33

2 Answers2

6

Your child routes are in the wrong place, they don't belong inside the options array, nor does the may_terminate key, try this...

'router' => array(
    'routes' => array(
        'app' => array(
            'type' => 'Zend\Mvc\Router\Http\Segment',
            'options' => array(
                'route' => '[/:info]/app',
                'defaults' => array(
                    '__NAMESPACE__' => 'X\App',
                    'controller' => 'Index',
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'example' => array(
                    'type' => 'Zend\Mvc\Router\Http\Segment',
                    'options' => array(
                        'route' => '/example[:/data]',
                        'defaults' => array(
                            'action' => 'example',
                        ),
                    ),
                ),
            ),
        ),
    ),
),
Crisp
  • 11,417
  • 3
  • 38
  • 41
1

You have got your syntax wrong

in the first example you have got your chil_routes definition inside your options array, it nees to be on the same level as the options array:

'router' => array(
    'routes' => array(
        'app' => array(
            'type' => 'Zend\Mvc\Router\Http\Segment',
            'options' => array(
                'route' => '[/:info]/app',
                'defaults' => array(
                    '__NAMESPACE__' => 'X\App',
                    'controller' => 'Index',
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'example' => array(
                    'type' => 'Zend\Mvc\Router\Http\Segment',
                    'options' => array(
                        'route' => '/example[:/data]',
                        'defaults' => array(
                            'action' => 'example',
                        ),
                    ),
                ),
            ),
        ),
    ),
Andrew
  • 12,617
  • 1
  • 34
  • 48
  • that was in fact the problem, but another arise: Fatal error: Uncaught exception 'Zend\Mvc\Router\Exception\RuntimeException' with message 'Found empty parameter name' in /home/X/zendframework/zendframework/library/Zend/Mvc/Router/Http/Segment.php:174 Stack trace: #0 /home/X/zendframework/zendframework/library/Zend/Mvc/Router/Http/Segment.php(106): Zend\Mvc\Router\Http\Segment->parseRouteDefinition('/example[:/data...') #1/home/X/vendor/zendframework/zendframework/library/Zend/Mvc/Router/Http/Segment.php(138): Zend\Mvc\Router\Http\Segment->__construct('/example[:/data...', Array, Array) #2 – MGP Mar 06 '13 at 15:23
  • 5
    `/example[:/data]` vs `/example[/:data]` maybe? – Crisp Mar 06 '13 at 15:28
  • 2
    I missed that too, the colon needs to be next to the word data.. :/data is not valid, /:data is valid – Andrew Mar 06 '13 at 15:32
  • 2
    You have the `/` after `:` so your param is effectively `/data` as opposed to just `data` – Crisp Mar 06 '13 at 15:32
  • Neither did I to begin with, slow day at the office though, more time to look carefully :) – Crisp Mar 06 '13 at 15:34
  • Damn.... I guess I really need another coffee to wake up... So many newbie mistakes and typos... – MGP Mar 06 '13 at 15:35