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

How to solve the problem that the sub-level of the left menu of yii2 cannot be highlighted, yii2 menu sub_PHP tutorial

WBOY
Release: 2016-07-12 08:53:43
Original
849 people have browsed it

How to solve the problem that the sub-level of the left menu of yii2 cannot be highlighted, yii2 menu sub-menu

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; 
<&#63;php 
$callback = function($menu){ 
 //鉴于篇幅有限,这里的代码省略,源码见于原文
}; 
//这里我们对一开始写的菜单menu进行了优化
echo dmstr\widgets\Menu::widget( [ 
 'options' => ['class' => 'sidebar-menu'], 
 'items' => MenuHelper::getAssignedMenu(Yii::$app->user->id, null, $callback), 
] ); &#63;>
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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),
]); 
Copy after login

Let’s go try it and see if our problem is solved.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1123780.htmlTechArticleHow to solve the problem that the sub-level of the left menu of yii2 cannot be highlighted. Let’s take a look at the specific problems of the yii2 menu first. . Adding a role belongs to the role menu. How to add a role...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template