Home > Article > Backend Development > How to solve the problem that the sub-level of the left menu in yii2 cannot be highlighted
Let’s take a 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 on the left navigation of adminlte cannot locate the index module (the secondary navigation on the left cannot be expanded and positioned)
If you follow our tutorial above, then the problem to be explained next should not be a problem , let’s first take a look at how we handled the left menu menu at that time
use mdm\admin\components\MenuHelper; <?php $callback = function($menu){ //鉴于篇幅有限,这里的代码省略,源码见于原文 }; //这里我们对一开始写的菜单menu进行了优化 echo dmstr\widgets\Menu::widget( [ 'options' => ['class' => 'sidebar-menu'], 'items' => MenuHelper::getAssignedMenu(Yii::$app->user->id, null, $callback), ] ); ?>
Seeing this, we might as well open the file dmstrwidgetsMenu to see how to implement the left menu selection, a problem that has troubled many students.
protected function isItemActive($item) { if (isset($item['url']) && is_array($item['url']) && isset($item['url'][0])) { //...... 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; } //...... return true; } return false; }
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? ? 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
protected function isItemActive($item) { if (isset($item['url']) && is_array($item['url']) && isset($item['url'][0])) { //...... //改写了路由的规则,是否高亮判断到controller而非action $routeCount = count($arrayRoute); if ($routeCount == 2) { if ($arrayRoute[0] !== $arrayThisRoute[0]) { return false; } } elseif ($routeCount == 3) { if ($arrayRoute[0] !== $arrayThisRoute[0]) { return false; } if (isset($arrayRoute[1]) && $arrayRoute[1] !== $arrayThisRoute[1]) { return false; } } else { return false; } // 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; // } //...... return true; } return false; }
It’s done, now the Menu file referenced by our left menu changes its point to backendcomponentsMenu
use backend\components\Menu; echo Menu::widget([ 'options' => ['class' => 'sidebar-menu'], 'items' => MenuHelper::getAssignedMenu(Yii::$app->user->id, null, $callback), ]);
Hurry and try it and see if our problem is solved.
The above introduces the method to solve the problem that the sub-level of the left menu of yii2 cannot be highlighted, including the methods of yii, I hope it will be helpful to friends who are interested in PHP tutorials.