Home >Backend Development >PHP Tutorial >yii2 left menu sub-level problem cannot be highlighted, yii2 menu sub-PHP tutorial
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