Solution to the problem that the Menu Sub-Level on the left of yii2 cannot be highlighted _ php instance

Source: Internet
Author: User
This article describes in detail how to solve the problem that cannot be highlighted in the menu sub-level on the left of yii2. If you are interested, please refer to the article for details.
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.