Home >Backend Development >PHP Tutorial >How to solve the problem that the sub-level of the left menu of yii2 cannot be highlighted, yii2 menu sub_PHP tutorial
Let’s take a look at the specific problem first.
Adding a role belongs to the role menu. How to make the role menu selected when performing the operation of adding a role?
The Create, View and other actions of adminlte's left navigation cannot locate the index module (the left secondary navigation 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
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 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? 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
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; }
You’re done, now the Menu file referenced by our left menu is modified to point to backendcomponentsMenu
use backend\components\Menu; echo Menu::widget([ 'options' => ['class' => 'sidebar-menu'], 'items' => MenuHelper::getAssignedMenu(Yii::$app->user->id, null, $callback), ]);
Let’s go try it and see if our problem is solved.