Table of Contents
yii2 左侧菜单子级无法高亮的问题,yii2菜单子
Home php教程 php手册 yii2 左侧菜单子级无法高亮的问题,yii2菜单子

yii2 左侧菜单子级无法高亮的问题,yii2菜单子

Jun 13, 2016 am 08:40 AM
yii2 left side question high

yii2 左侧菜单子级无法高亮的问题,yii2菜单子

作者:白狼 出处:http://www.manks.top/question/20160508000001.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

前面我们写的文章yii2 rbac权限控制之菜单menu详细教程,很完美,也很实用,鉴于不少小伙伴咨询的问题,这里做一个统一的说明。我们先来看看具体问题。

添加角色是属于角色这个菜单的,如何在执行添加角色这个操作时让角色这个菜单处于选中状态呢?
adminlte左侧导航的Create,View等action不能定位到index的模块(左侧二级导航不能展开定位)
如果你是按照我们上文的教程来的,那接下来所要说明的问题应该不是问题,先来看看我们当时是怎么处理左侧菜单menu的

<span>use</span><span> mdm\admin\components\MenuHelper; 
</span><?<span>php 
</span><span>$callback</span> = <span>function</span>(<span>$menu</span><span>){ 
    </span><span>//</span><span>鉴于篇幅有限,这里的代码省略,源码见于原文</span>
<span>}; 
</span><span>//</span><span>这里我们对一开始写的菜单menu进行了优化</span>
<span>echo</span> dmstr\widgets\Menu::<span>widget( [ 
    </span>'options' => ['class' => 'sidebar-menu'], 
    'items' => MenuHelper::getAssignedMenu(Yii::<span>$app</span>->user->id, <span>null</span>, <span>$callback</span>),<span> 
] ); </span>?>
Copy after login

看到这里,我们不妨打开文件dmstr\widgets\Menu看看这里是怎么实现左侧菜单选中这一困扰众多同学的问题。

<span>protected</span> <span>function</span> isItemActive(<span>$item</span><span>)
{
    </span><span>if</span> (<span>isset</span>(<span>$item</span>['url']) && <span>is_array</span>(<span>$item</span>['url']) && <span>isset</span>(<span>$item</span>['url'][0<span>])) {
        </span><span>//</span><span>......</span>
        <span>if</span> (<span>$arrayRoute</span>[0] !== <span>$arrayThisRoute</span>[0<span>]) {
            </span><span>return</span> <span>false</span><span>;
        }
        </span><span>if</span> (<span>isset</span>(<span>$arrayRoute</span>[1]) && <span>$arrayRoute</span>[1] !== <span>$arrayThisRoute</span>[1<span>]) {
            </span><span>return</span> <span>false</span><span>;
        }
        </span><span>if</span> (<span>isset</span>(<span>$arrayRoute</span>[2]) && <span>$arrayRoute</span>[2] !== <span>$arrayThisRoute</span>[2<span>]) {
            </span><span>return</span> <span>false</span><span>;
        }
        </span><span>//</span><span>......</span>
        <span>return</span> <span>true</span><span>;
    }
    </span><span>return</span> <span>false</span><span>;
}</span>
Copy after login

看吧,看上面的代码,也就是说左侧菜单激活的情况是当前路由完全等于菜单路由时菜单才进行激活。

鉴于我们一开始谈到的不少小伙伴疑惑的两个问题,我们这里只需要稍稍调整下代码,判断控制到controller而非action即可,但是源码文件我们又不能修改,怎么办好呢?天热,凉拌。

这里我们拷贝dmstr\widgets\Menu.php文件到backend\components\Menu.php,然后按照下面的方法修改isItemActive方法即可

<span>protected</span> <span>function</span> isItemActive(<span>$item</span><span>)
{
    </span><span>if</span> (<span>isset</span>(<span>$item</span>['url']) && <span>is_array</span>(<span>$item</span>['url']) && <span>isset</span>(<span>$item</span>['url'][0<span>])) {
        
        </span><span>//</span><span>......


        //改写了路由的规则,是否高亮判断到controller而非action</span>
        <span>$routeCount</span> = <span>count</span>(<span>$arrayRoute</span><span>);
        </span><span>if</span> (<span>$routeCount</span> == 2<span>) {
            </span><span>if</span> (<span>$arrayRoute</span>[0] !== <span>$arrayThisRoute</span>[0<span>]) {
                </span><span>return</span> <span>false</span><span>;
            }
        } </span><span>elseif</span> (<span>$routeCount</span> == 3<span>) {
            </span><span>if</span> (<span>$arrayRoute</span>[0] !== <span>$arrayThisRoute</span>[0<span>]) {
                </span><span>return</span> <span>false</span><span>;
            }
            </span><span>if</span> (<span>isset</span>(<span>$arrayRoute</span>[1]) && <span>$arrayRoute</span>[1] !== <span>$arrayThisRoute</span>[1<span>]) {
                </span><span>return</span> <span>false</span><span>;
            }
        } </span><span>else</span><span> {
            </span><span>return</span> <span>false</span><span>;
        }


        </span><span>//</span><span> if ($arrayRoute[0] !== $arrayThisRoute[0]) {
        //     return false;
        // }
        // if (isset($arrayRoute[1]) && $arrayRoute[1] !== $arrayThisRoute[1]) {
        //     return false;
        // }
        // if (isset($arrayRoute[2]) && $arrayRoute[2] !== $arrayThisRoute[2]) {
        //     return false;
        // }
        
        //......</span>
        
        <span>return</span> <span>true</span><span>;
    }
    </span><span>return</span> <span>false</span><span>;
}</span>
Copy after login

大功告成,现在我们左侧的菜单引用的Menu文件修改其指向到backend\components\Menu

<span>use</span><span> backend\components\Menu;
</span><span>echo</span> Menu::<span>widget([
    </span>'options' => ['class' => 'sidebar-menu'],
    'items' => MenuHelper::getAssignedMenu(Yii::<span>$app</span>->user->id, <span>null</span>, <span>$callback</span>),<span>
]); </span>
Copy after login

 

快去试试看我们的问题解决没有吧。

menuyii2

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solve the 'error: redefinition of class 'ClassName'' problem that appears in C++ code Solve the 'error: redefinition of class 'ClassName'' problem that appears in C++ code Aug 25, 2023 pm 06:01 PM

Solve the "error:redefinitionofclass'ClassName'" problem in C++ code. In C++ programming, we often encounter various compilation errors. One of the common errors is "error:redefinitionofclass 'ClassName'" (redefinition error of class 'ClassName'). This error usually occurs when the same class is defined multiple times. This article will

Clustering effect evaluation problem in clustering algorithm Clustering effect evaluation problem in clustering algorithm Oct 10, 2023 pm 01:12 PM

The clustering effect evaluation problem in the clustering algorithm requires specific code examples. Clustering is an unsupervised learning method that groups similar samples into one category by clustering data. In clustering algorithms, how to evaluate the effect of clustering is an important issue. This article will introduce several commonly used clustering effect evaluation indicators and give corresponding code examples. 1. Clustering effect evaluation index Silhouette Coefficient Silhouette coefficient evaluates the clustering effect by calculating the closeness of the sample and the degree of separation from other clusters.

What to do if win10 cannot download steam What to do if win10 cannot download steam Jul 07, 2023 pm 01:37 PM

Steam is a very popular game platform with many high-quality games, but some win10 users report that they cannot download steam. What is going on? It is very likely that the user's IPv4 server address is not set properly. To solve this problem, you can try to install Steam in compatibility mode, and then manually modify the DNS server to 114.114.114.114, and you should be able to download it later. What to do if Win10 cannot download Steam: Under Win10, you can try to install it in compatibility mode. After updating, you must turn off compatibility mode, otherwise the web page will not load. Click the properties of the program installation to run the program in compatibility mode. Restart to increase memory, power

Teach you how to diagnose common iPhone problems Teach you how to diagnose common iPhone problems Dec 03, 2023 am 08:15 AM

Known for its powerful performance and versatile features, the iPhone is not immune to the occasional hiccup or technical difficulty, a common trait among complex electronic devices. Experiencing iPhone problems can be frustrating, but usually no alarm is needed. In this comprehensive guide, we aim to demystify some of the most commonly encountered challenges associated with iPhone usage. Our step-by-step approach is designed to help you resolve these common issues, providing practical solutions and troubleshooting tips to get your equipment back in peak working order. Whether you're facing a glitch or a more complex problem, this article can help you resolve them effectively. General Troubleshooting Tips Before delving into specific troubleshooting steps, here are some helpful

Solve PHP error: problems encountered when inheriting parent class Solve PHP error: problems encountered when inheriting parent class Aug 17, 2023 pm 01:33 PM

Solving PHP errors: Problems encountered when inheriting parent classes In PHP, inheritance is an important feature of object-oriented programming. Through inheritance, we can reuse existing code and extend and improve it without modifying the original code. Although inheritance is widely used in development, sometimes you may encounter some error problems when inheriting from a parent class. This article will focus on solving common problems encountered when inheriting from a parent class and provide corresponding code examples. Question 1: The parent class is not found. During the process of inheriting the parent class, if the system does not

How to solve the problem that jQuery cannot obtain the form element value How to solve the problem that jQuery cannot obtain the form element value Feb 19, 2024 pm 02:01 PM

To solve the problem that jQuery.val() cannot be used, specific code examples are required. For front-end developers, using jQuery is one of the common operations. Among them, using the .val() method to get or set the value of a form element is a very common operation. However, in some specific cases, the problem of not being able to use the .val() method may arise. This article will introduce some common situations and solutions, and provide specific code examples. Problem Description When using jQuery to develop front-end pages, sometimes you will encounter

Label acquisition problem in weakly supervised learning Label acquisition problem in weakly supervised learning Oct 08, 2023 am 09:18 AM

The label acquisition problem in weakly supervised learning requires specific code examples. Introduction: Weakly supervised learning is a machine learning method that uses weak labels for training. Different from traditional supervised learning, weakly supervised learning only needs to use fewer labels to train the model, rather than each sample needs to have an accurate label. However, in weakly supervised learning, how to accurately obtain useful information from weak labels is a key issue. This article will introduce the label acquisition problem in weakly supervised learning and give specific code examples. Introduction to the label acquisition problem in weakly supervised learning:

The problem of generalization ability of machine learning models The problem of generalization ability of machine learning models Oct 08, 2023 am 10:46 AM

The generalization ability of machine learning models requires specific code examples. With the development and application of machine learning becoming more and more widespread, people are paying more and more attention to the generalization ability of machine learning models. Generalization ability refers to the prediction ability of a machine learning model on unlabeled data, and can also be understood as the adaptability of the model in the real world. A good machine learning model should have high generalization ability and be able to make accurate predictions on new data. However, in practical applications, we often encounter models that perform well on the training set, but fail on the test set or real

See all articles