自动设置活动项目

时间:2018-08-13 07:37:23

标签: menu yii2 yii2-widget

我的应用程序中有一个菜单:

<?php
NavBar::begin([
    'brandLabel' => Html::img('@web/images/logo-top.png', ['id' => 'logo']),
    'brandUrl' => Yii::$app->homeUrl,
    'options' => [
        'class' => 'navbar-inverse navbar-static-top',
    ],
]);

if (count($menuItems)) {
    echo Nav::widget([
        'options' => ['class' => 'navbar-nav'],
        'items' => $menuItems,
    ]);
}
?>

<?php NavBar::end(); ?>

$menuItems在控制器内生成:

private function constructMenu($categories) {
    $menu = [];

    if (is_array($categories) && (count($categories) > 0)) {
        foreach($categories as $key => $category) {
            $menu[$key] = [
                'label' => $category['name'],
                'url' => Url::to([
                    'category/view',
                    'slug' => $category['slug']
                ]),
            ];
            if (is_array($category['children']) && (count($category['children']) > 0)) {
                $menu[$key]['items'] = $this->constructMenu($category['children']);
            }
        }
    }

    return $menu;
}

我也有urlManager配置:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        'category/<slug:[\w_-]+>' => 'category/view',
        'item/<slug:[\w_-]+>' => 'item/view',
        'cart/remove/<item_id:\d+>' => 'cart/remove',
        'cart/add/<item_id:\d+>' => 'cart/add',
    ],
],

因此,唯一的问题是菜单项始终为active = false。如何修改constructMenu方法以为active键设置适当的值?还是我应该在模板中完成?

以下是来自PhpStrom调试面板的$menuItems的内容,即将传递给Nav::widget之前:

$menuItems variable content

这是我现在的位置:

NavBar::begin([
    'brandLabel' => Html::img('@web/images/logo-top.png',
        ['id' => 'logo', 'style' => 'height: 40px; filter: invert(100%);']),
    'brandUrl' => Yii::$app->homeUrl,
    'options' => [
        'class' => 'navbar-inverse navbar-static-top',
    ],
]);

$controllerAndSlug = $this->context->id . '/' . $this->context->actionParams['slug'];
$menuItems = array_map(
    function($item) use ($controllerAndSlug) {
        $item['active'] = strpos($item['url'], $controllerAndSlug) !== false;
        return $item;
    },
    $menuItems
);

if (count($menuItems)) {
    echo Nav::widget([
        'options' => ['class' => 'navbar-nav'],
        'items' => $menuItems,
    ]);
}
NavBar::end();

它工作正常,但仅在顶级菜单项上设置active标志。现在,我想知道如何将参数传递给回调函数。

2 个答案:

答案 0 :(得分:1)

尽管如果您以/controller/action格式提供完整的URL,但如果仍然无法使用,则可以使用,但是您可以通过检查当前控制器使用active的{​​{1}}选项,动作和弹

item

答案 1 :(得分:0)

尝试一下:

private function constructMenu($categories) {
    $menu = [];

    if (is_array($categories) && (count($categories) > 0)) {
        foreach($categories as $key => $category) {
            $menu[$key] = [
                'label' => $category['name'],
                'url' => Url::to([
                    'category/view',
                    'slug' => $category['slug']
                ]),
                'active' => Yii::$app->controller->id == 'category' 
                     && Yii::$app->controller->action->id == 'view'
                     && Yii::$app->request->get('slug') == $category['slug']
            ];
            if (is_array($category['children']) && (count($category['children']) > 0)) {
                $menu[$key]['items'] = $this->constructMenu($category['children']);
            }
        }
    }

    return $menu;
}