Heim  >  Artikel  >  Backend-Entwicklung  >  yii2 左侧菜单子级无法高亮的问题,yii2菜单子_PHP教程

yii2 左侧菜单子级无法高亮的问题,yii2菜单子_PHP教程

WBOY
WBOYOriginal
2016-07-12 08:53:18708Durchsuche

yii2 左侧菜单子级无法高亮的问题,yii2菜单子

作者:白狼 出处:http://www.manks.top/question/20160508000001.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

前面我们写的文章yii2 rbac权限控制之菜单menu详细教程,很完美,也很实用,鉴于不少小伙伴咨询的问题,这里做一个统一的说明。我们先来看看具体问题。

添加角色是属于角色这个菜单的,如何在执行添加角色这个操作时让角色这个菜单处于选中状态呢?
adminlte左侧导航的Create,View等action不能定位到index的模块(左侧二级导航不能展开定位)
如果你是按照我们上文的教程来的,那接下来所要说明的问题应该不是问题,先来看看我们当时是怎么处理左侧菜单menu的

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

 

看到这里,我们不妨打开文件dmstr\widgets\Menu看看这里是怎么实现左侧菜单选中这一困扰众多同学的问题。

<span>protected</span> <span>function</span> isItemActive(<span>$item</span><span>)
{
    </span><span>if</span> (<span>isset</span>(<span>$item</span>['url']) && <span>is_array</span>(<span>$item</span>['url']) && <span>isset</span>(<span>$item</span>['url'][0<span>])) {
        </span><span>//</span><span>......</span>
        <span>if</span> (<span>$arrayRoute</span>[0] !== <span>$arrayThisRoute</span>[0<span>]) {
            </span><span>return</span> <span>false</span><span>;
        }
        </span><span>if</span> (<span>isset</span>(<span>$arrayRoute</span>[1]) && <span>$arrayRoute</span>[1] !== <span>$arrayThisRoute</span>[1<span>]) {
            </span><span>return</span> <span>false</span><span>;
        }
        </span><span>if</span> (<span>isset</span>(<span>$arrayRoute</span>[2]) && <span>$arrayRoute</span>[2] !== <span>$arrayThisRoute</span>[2<span>]) {
            </span><span>return</span> <span>false</span><span>;
        }
        </span><span>//</span><span>......</span>
        <span>return</span> <span>true</span><span>;
    }
    </span><span>return</span> <span>false</span><span>;
}</span>

 

看吧,看上面的代码,也就是说左侧菜单激活的情况是当前路由完全等于菜单路由时菜单才进行激活。

鉴于我们一开始谈到的不少小伙伴疑惑的两个问题,我们这里只需要稍稍调整下代码,判断控制到controller而非action即可,但是源码文件我们又不能修改,怎么办好呢?天热,凉拌。

这里我们拷贝dmstr\widgets\Menu.php文件到backend\components\Menu.php,然后按照下面的方法修改isItemActive方法即可

<span>protected</span> <span>function</span> isItemActive(<span>$item</span><span>)
{
    </span><span>if</span> (<span>isset</span>(<span>$item</span>['url']) && <span>is_array</span>(<span>$item</span>['url']) && <span>isset</span>(<span>$item</span>['url'][0<span>])) {
        
        </span><span>//</span><span>......


        //改写了路由的规则,是否高亮判断到controller而非action</span>
        <span>$routeCount</span> = <span>count</span>(<span>$arrayRoute</span><span>);
        </span><span>if</span> (<span>$routeCount</span> == 2<span>) {
            </span><span>if</span> (<span>$arrayRoute</span>[0] !== <span>$arrayThisRoute</span>[0<span>]) {
                </span><span>return</span> <span>false</span><span>;
            }
        } </span><span>elseif</span> (<span>$routeCount</span> == 3<span>) {
            </span><span>if</span> (<span>$arrayRoute</span>[0] !== <span>$arrayThisRoute</span>[0<span>]) {
                </span><span>return</span> <span>false</span><span>;
            }
            </span><span>if</span> (<span>isset</span>(<span>$arrayRoute</span>[1]) && <span>$arrayRoute</span>[1] !== <span>$arrayThisRoute</span>[1<span>]) {
                </span><span>return</span> <span>false</span><span>;
            }
        } </span><span>else</span><span> {
            </span><span>return</span> <span>false</span><span>;
        }


        </span><span>//</span><span> 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;
        // }
        
        //......</span>
        
        <span>return</span> <span>true</span><span>;
    }
    </span><span>return</span> <span>false</span><span>;
}</span>

 

大功告成,现在我们左侧的菜单引用的Menu文件修改其指向到backend\components\Menu

<span>use</span><span> backend\components\Menu;
</span><span>echo</span> Menu::<span>widget([
    </span>'options' => ['class' => 'sidebar-menu'],
    'items' => MenuHelper::getAssignedMenu(Yii::<span>$app</span>->user->id, <span>null</span>, <span>$callback</span>),<span>
]); </span>

 

快去试试看我们的问题解决没有吧。

menuyii2

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1125073.htmlTechArticleyii2 左侧菜单子级无法高亮的问题,yii2菜单子 作者:白狼 出处:http://www.manks.top/question/20160508000001.html本文版权归作者,欢迎转载,但未经...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn