Yii2 left menu sub-level cannot be highlighted-Wolf stack

Source: Internet
Author: User
Yii2 left menu sub-level can not highlight the problem-White Wolf stack author: White Wolf Source: http://www.manks.top/question/20160508000001.html this article copyright to the author, welcome to reprint, but without the author's consent must retain this statement, the original article connection is clearly provided on the article page, otherwise the right to pursue legal liability will be reserved.

In the previous article, we wrote a detailed tutorial on the menu of rbac permission control in yii2, which is perfect and practical. In view of the questions consulted by many small Partners, a unified description is provided here. 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;
 ['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.

Menuyii2

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.