Search code examples
zend-framework2zend-navigation

Zend 2 Navigation rendering active class missing


i do have a problem regarding rendering the navigation in zf2. I have not found anybody else having this issue. But maybe you have a clue.

Althought everything seems to be configured right, due to the fact that everything does work, rendering zend navigation does not mark the active route with class="active".

This is part of my module.config.php:

'service_manager' => array(
        'factories' => array(
            'app_navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
            'User\Acl\Service' => 'User\Acl\ServiceFactory',
            'User\Auth\Service' => 'User\Authentication\ServiceFactory',
        ),
    ),

//global config key for all navigation configurations
'navigation' => array(
     //name of the DefaultNavigation created by DefaultNavigationFactory
     'default' => array(
         //config of first page
         'welcome' => array(
             'label' => 'Home',
             'route' => 'welcome',
             'controller' => 'People\Controller\PeopleController',
             'action' => 'welcome',
             'type' => 'uri',
             'uri' => '/welcome',
             'module'    => 'TheGlobalDatabase',
         ),
...

This is the route:

     'router' => array(       
        'routes' => array(
           'welcome' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/welcome',
                    'defaults' => array(
                        'controller' => 'People\Controller\People',
                        'action'     => 'welcome',
                    ),
                ),
            ),

I do echo the navigation in the layout like this:

echo $this->navigation()->menu()->renderMenu('app_navigation',array('ulClass'=>'nav navbar-nav welcome')); 

Maybe you have a hint, what else to check.

Thank you all!


Solution

  • You have missing or incorrect config in both route and navigation.

    Navigation

    • You will never need to use the type param when using array config. It is the navigation factory's job to determine if the type is either uri or mvc. Just remove the config for it.

    • The navigation controller Should be the registered service name of the controller, not the fully qualified class name. 'controller' => 'People\Controller\People'.

    • Also You do not need to specify the controller and action in the navigation config when using a route param.

    With these changes, the navigation is very simple.

     'welcome' => array(
       'label' => 'Home',
       'route' => 'welcome'
     );
    

    Route

    It could be a Literal route (as there are no parameters being passed in) and you are missing the may_terminate argument which will be required if you wish to match on your route.

    'welcome' => array(
        'type'    => 'literal',
        'options' => array(
            'route'    => '/welcome',
            'defaults' => array(
                'controller' => 'People\Controller\People',
                'action'     => 'welcome',
            ),
        ),
        'may_terminate' => true,
    ),