如果子路线在导航中处于活动状态,则激活条目

时间:2017-03-14 10:50:53

标签: zend-framework2 zend-framework3

我在配置中定义了以下路由。我的主导航中只有一个级别,并且如果指定的导航路径的子路径匹配,则要激活导航条目。这可能吗?

在我的示例中,当我访问Leads路线的任何子路线(例如/ lead / list)时,导航条目lead应该处于活动状态。它也应该适用于更深层次的子路线(例如/ lead / list / super / page)。

路线

'routes' => [
    'lead' => [
        'type' => Literal::class,

        'options' => [
            'route' => '/lead'
        ],

         'child_routes' => [
             'list' => [
                 'type' => Literal::class,
                 'may_terminate' => true,

                 'options' => [
                     'route' => '/list',

                     'defaults' => [
                         'controller' => Controller\Lead\ListController::class,
                         'action' => 'index'
                     ]
                 ]
             ],

             'view' => [
                 'type' => Segment::class,
                 'may_terminate' => true,

                 'options' => [
                     'route' => '/:id',

                     'defaults' => [
                         'controller' => Controller\Lead\ViewController::class,
                         'action' => 'index'
                     ]
                 ]
             ]
         ]
     ]
]

导航

'default' => [
    'lead' => [
        'label' => 'Leads',
        'route' => 'lead',
    ]
]

1 个答案:

答案 0 :(得分:0)

您必须像定义路线一样配置导航列表 的路线

      'navigation' => [
            'type'    => Literal::class,
            'options' => [
                'route'    => '/navigation',
                'defaults' => [
                    'controller' => Controller\IndexController::class,
                    'action'     => 'index',
                ],
            ],
            'may_terminate' => true,
            'child_routes' => [
                'child' => [
                    'type' => Literal::class,
                    'options' => [
                        'route' => '/child',
                        'defaults' => [
                            'controller' => Controller\IndexController::class,
                            'action' => 'index'
                        ]
                    ],
                    'may_terminate' => true,
                    'child_routes' => [
                        'child' => [
                            'type' => Literal::class,
                            'options' => [
                                'route' => '/child',
                                'defaults' => [
                                    'controller' => Controller\IndexController::class,
                                    'action' => 'index'
                                ]
                            ]
                        ],
                    ]
                ],
            ]
        ],

导航

return [
    'navigation' => [
        'default' => [
            [
                'label' => 'Home',
                'route' => 'home',
            ],
            [
                'label' => 'Navigation',
                'route' => 'navigation',
                'pages' => [
                    [
                        'label' => 'Child #1',
                        'route' => 'navigation/child',
                        'pages' => [
                            [
                                'label' => 'Child #2',
                                'route' => 'navigation/child/child',
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],
    'service_manager' => [
        'factories' => [
            'navigation' => Zend\Navigation\Service\DefaultNavigationFactory::class,
        ],
    ],
];

现在,当您访问时:
/naviation - 仅导航处于活动状态 /navigation/child - 导航和孩子活跃了 /navigation/child/child - 导航,儿童和儿童都有效

如果您想要只渲染活动分支,可以在视图中使用

<?= $this->navigation('navigation')->menu()->setOnlyActiveBranch(true); ?>

或者,如果要自定义位菜单,可以使用接受2个参数的renderMenu()方法。 $container$options,因此您可以为菜单添加自己的类或设置缩进。

您还可以根据提供的部分

使用renderPartial()和渲染菜单