ZF2: Active branch for child routes in zend naviga

2019-05-14 03:32发布

问题:

I have this as route configuration in Zend Framework 2:

'news' => array(
    'type' => 'Literal',
    'options' => array(
        'route' => '/news',
        'defaults' => array(
            '__NAMESPACE__' => __NAMESPACE__ . '\Controller',
            'controller' => 'News',
            'action' => 'index',
            'page' => 1,
        ),
    ),
    'may_terminate' => true,
    'child_routes' => array(
        'index' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '[/:page]',
                'constraints' => array(
                    'page' => '\d+',
                ),
                'defaults' => array(
                    '__NAMESPACE__' => __NAMESPACE__ . '\Controller',
                    'controller' => 'News',
                    'action' => 'index',
                    'page' => 1,
                ),
            ),
        ),
    ),
),

I also have a navigation where an element points to route name news.

When I am on the /news page everything is fine and the news navigation element is active. But when I am on /news/2 which matches route news/index the navigation element isn't active.

How can I tell it to be active for every child route of the route it is bound to?

回答1:

Have you set controller and action inside your naviation settings? If you set those it should match the news route no matter which page is set because the action/controller match:

'pages' => array(
     array(
        'label'        => 'News',
        'route'        => 'news',
        'controller'   => 'news',
        'action'      => 'index',
    ),
)