1
votes

I was looking for show a navigation menu in the layout.phtml file:

<?php echo $this->navigation('navigation')->menu(); ?>

So, I added these lines to /module/Application/config/module.config.php in order to declare the menu's structure according to my app routes:

'navigation' => array(
        'default' => array(
            'place' => array(
                'label' => 'Places',
                'route' => '/place',
                'pages' => array(
                    'country' => array(
                        'label' => 'Countries',
                        'route' => '/place/country',
                    ),
                    'state' => array(
                        'label' => 'States',
                        'route' => '/place/state',
                    ),
                    'city' => array(
                        'label' => 'Cities',
                        'route' => '/place/city',
                    ),
                ),
            ),
        ),
     ),

and then I wrote a loader for the Zend/Navigation instance:

'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
            'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
        ),
    ),

but I get repeatedly this exception:

Fatal error: Zend\Mvc\Router\Exception\RuntimeException: Route with name "" not found in C:\wamp\www\bsol\vendor\zendframework\zendframework\library\Zend\View\Helper\Navigation\AbstractHelper.php on line 471

I attempted to solve the problem adding this line to the menu declaration:

             'pages' => array(
                    'route' => '/place',
                    'country' => array(
                        'label' => 'Countries',
                        'route' => '/place/country',
                    ),

Nevertheless, In that case I get another different exception:

Fatal error: Uncaught exception 'Zend\Navigation\Exception\InvalidArgumentException' with message 'Invalid argument: $page must be an instance of Zend\Navigation\Page\AbstractPage or Traversable, or an array' in C:\wamp\www\bsol\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php on line 733

UPDATED: Following Jurian Sluiman's suggestion,

This must be my router configuration (module\Place\config\module.config.php), if I've got a module called 'Place', with 3 controllers (city, country and state):

'router' => array(
        'routes' => array(
            'city' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/city[/:action][/:oid]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                        'controller' => 'Place\Controller\City',
                        'action'     => 'index',
                    ),
                ),
            ),

            'country' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/country[/:action][/:oid]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                        'controller' => 'Place\Controller\Country',
                        'action'     => 'index',
                    ),
                ),
            ),

            'state' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/state[/:action][/:oid]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                        'controller' => 'Place\Controller\State',
                        'action'     => 'index',
                    ),
                ),
            ),

        ),
),

And this must be my navigation config (actually I located this one in an autoloadable file in /config/autoload/menu.global.php):

return array(
    'navigation' => array(
        'default' => array(
            'place' => array(
                'label' => 'Places',
                'route' => 'country',
                'pages' => array(
                    'country' => array(
                        'label' => 'Countries',
                        'route' => 'country',
                    ),
                    'state' => array(
                        'label' => 'States',
                        'route' => 'state',
                    ),
                    'city' => array(
                        'label' => 'Cities',
                        'route' => 'city',
                    ),
                ),
            ),
        ),
     ),
);

...And now It works!.

1

1 Answers

5
votes

Usually routes start not with a /. The / is a delimiter for route names, so when the /place route is assembled, you have a parent "" and then a child "place". The first one is empty, giving you this error.

Perhaps you confused route names with the route itself. You have to provide the name of the route, so the route itself (the URL) will be returned. So imagine you have this route config:

'router' => array(
    'routes' => array(
        'place' => array(
            'type' => 'literal',
            'options' => array(
                'route'    => '/place',
                'defaults' => array(
                    // Some defaults here
                ),
            ),
        ),
    ),
),

In this case, your route name is place and not /place. If you have a different route config then the one here above, put the route config in your question so I can update my answer.