Workaround _php instance of the Yii2 left menu child cannot highlight problem

Source: Internet
Author: User
Let's take a look at the specific question first.
Adding roles is part of the role menu, how do you make the role menu selected when you perform the add role?
Adminlte left navigation Create,view etc. action cannot be positioned to index module (left level two navigation cannot be positioned)
If you follow our previous tutorial, then the next question should not be a problem, first to see how we handled the menu on the left side of the

Use Mdm\admin\components\menuhelper; <?php $callback = function ($menu) {  //due to space limitations, the code here is omitted, the source is found in the original text};//Here we have optimized the Echo Dmstr\widgets\menu for the menu we started writing: : Widgets ([  ' options '] = [' class ' = ' Sidebar-menu '],  ' Items ' = Menuhelper::getassignedmenu (yii::$ App->user->id, NULL, $callback),]);?>

See here, we might as well open the file Dmstr\widgets\menu see here is how to implement the left menu to select this problem that bothers many classmates.

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, the left menu is activated when the current route is exactly equal to the menu route when the menu is activated.

In view of the many small partners we talked about at the beginning of the two questions, we only need to slightly adjust the code, to control the controller and not the action, but the source file we can not modify, how to do? It's hot, salad.

Here we copy the dmstr\widgets\menu.php file to backend\components\menu.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])) {    //...  Overrides the rules for routing, whether the controller is highlighted and not the action  $routeCount = count ($arrayRoute);  if ($routeCount = = 2) {   if ($arrayRoute [0]!== $arrayThisRoute [0]) {    return false;   }  } elseif ($routeCoun t = = 3) {   if ($arrayRoute [0]!== $arrayThisRoute [0]) {    return false;   }   if (Isset ($arrayRoute [1]) && $arrayRoute [1]!== $arrayThisRoute [1]) {    return false;   }  } else { C15/>return false;  }  if ($arrayRoute [0]!== $arrayThisRoute [0]) {  //  return false;  }  //if (Isset ($arrayRoute [1]) && $arrayRoute [1]!== $arrayThisRoute [1]) {  //  return false;< c24/>//}  //if (Isset ($arrayRoute [2]) && $arrayRoute [2]!== $arrayThisRoute [2]) {  //  return false;  // }    //......    return true; } return false;}

Now that we're done, the menu file on our left-hand side changes its point to Backend\components\menu

Go ahead and try our problem solving.

  • Related Article

    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.