


yii2 left menu sub-level problem cannot be highlighted, yii2 menu sub-PHP tutorial
yii2 The problem that the left menu sub-level cannot be highlighted, yii2 menu sub-sub
Author: Bailang Source: http://www.manks.top/question/20160508000001. html The copyright of this article belongs to the author, and you are welcome to reprint it. However, this statement must be retained without the author's consent, and a link to the original text must be provided in an obvious position on the article page. Otherwise, we reserve the right to pursue legal liability.
The article we wrote earlier about the detailed tutorial on the menu of yii2 rbac permission control is perfect and very practical. In view of the questions that many friends have asked, here is a unified explanation. Let’s look at the specific issues first.
Adding a character belongs to the character menu. How to make the character menu selected when performing the operation of adding a character?
Create, View and other actions in the adminlte left navigation cannot locate the index module (the left secondary navigation cannot be expanded and positioned)
If you follow our tutorial above, then what is to be explained next The problem shouldn’t be a problem, let’s first take a look at how we handled the left menu 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>?>
Seeing this, we might as well open the file dmstrwidgetsMenu to see how to implement left menu selection, a problem that has troubled many students.
<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>
Look, look at the code above, that is to say, the menu on the left is activated when the current route is completely equal to the menu route.
In view of the two questions we talked about at the beginning that many friends were confused about, we only need to slightly adjust the code here to determine whether the control is controlled by the controller instead of the action. However, we cannot modify the source code file. What should we do? Woolen cloth? When the weather is hot, serve it cold.
Here we copy the dmstrwidgetsMenu.php file to backendcomponentsMenu.php, and then modify the isItemActive method as follows
<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>
You’re done, now the Menu file referenced by our left menu is modified to point to backendcomponentsMenu
<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>
Let’s go try it and see if our problem is solved.
menuyii2

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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

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

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

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

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

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 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
