This article describes in detail how to solve the problem that the menu on the left of yii2 cannot be highlighted. if you are interested, refer to Yii2.
Let's take a look at the specific problem.
Adding a role belongs to the role menu. how can I select the role menu when adding a role?
Actions such as Create and View in the left-side navigation bar of adminlte cannot locate the index module (the left-side navigation bar cannot expand and locate)
If you follow the tutorial above, the problem to be explained next should not be a problem. let's take a look at how we handled the menu on the left.
Use mdm \ admin \ components \ MenuHelper; <? Php $ callback = function ($ menu) {// in view of limited space, the code here is omitted, and the source code is found in the original article }; // Here we have optimized echo dmstr \ widgets \ menu :: widget (['options' => ['class' => 'sidebar-menu '], 'items' => MenuHelper: getAssignedMenu (Yii :: $ app-> user-> id, null, $ callback),]);?>
Here, we may open the dmstr \ widgets \ Menu file to see how the Menu on the left is selected.
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 at the code above. that is to say, the menu on the left is activated only when the current route is completely equal to the menu route.
In view of the two questions we talked about at the beginning, we only need to slightly adjust the code here to control the controller rather than the action, but we cannot modify the source code file, what should I do? Hot and cold.
Copy the dmstr \ widgets \ Menu. php file to backend \ components \ Menu. php, and modify the isItemActive method as follows.
Protected function isItemActive ($ item) {if (isset ($ item ['URL']) & is_array ($ item ['URL']) & isset ($ item ['URL'] [0]) {// ...... // rewrite the routing rule, whether to highlight the controller instead of 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 ;}
Now, the Menu file referenced by the Menu on the left is directed to backend \ components \ Menu.
use backend\components\Menu;echo Menu::widget([ 'options' => ['class' => 'sidebar-menu'], 'items' => MenuHelper::getAssignedMenu(Yii::$app->user->id, null, $callback),]);
Are you sure you want to solve the problem.