yii2 利用小部件生成後臺左邊菜單欄

    ************   模型層遞歸查詢權限   ************php

/**
     * 遞歸方式查詢權限
     */
    public function getPrivilege()
    {
        $connection = \Yii::$app->db;
        $top=$command = $connection->createCommand('SELECT * FROM privilege')->queryAll();       
        return $this->digui($top, $parent_id=0);
    }
    
    public function digui($top, $parent_id)
    {   
        $child = array();
        foreach ($top as $key => $v)
        {
            if($v['parent_id'] == $parent_id)
            {
                $child[] = $v;
            }
        }
        if(empty($child))
        {
            return  null;
        }
        foreach($child as $key => $v)
        {
            $second = $this->digui($top, $v['p_id']);
            if($second)
            {
                $child[$key]['child'] = $second;
            }
        }
        return $child;
    }
html

      ************   控制器層組合數組   ************bootstrap

public function actionLeft(){
        
        //查詢全部權限
        $pri = new Privilege();
        $privilege = $pri->getPrivilege();
        $child = array();
        foreach ($privilege as $key => $val) {
            if($val['child']){
                foreach ($val['child'] as $k => $v) {
                    $child[$key][] = "<a href='index.php?r=".$v['controller'].'/'.$v['action']."' target='mainFrame' >".$v['privilege']."</a>";
                }
            } else {
                unset($privilege[$key]);
            }
        }
        //print_r($child);die;
        return $this->render('left',['privilege'=>$privilege,'child'=>$child]);
    }
數組

************   視圖層引用部件   ************app

<?php

//摺疊
use yii\bootstrap\Collapse;

?>

<style>
   #yii-debug-toolbar{display: none;}
</style>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>項目後臺管理系統</title>
</head>
<body style="background:#4AA3D8">
<?php foreach($privilege as $k => $v){?>
    <?php
    echo  Collapse::widget([
    'items'=>[
        [
            'label'=>$v['privilege'],
            'content'=>$child[$k],
        ]        
    ],
    'options'=>['style'=>['margin-bottom'=>'5px', 'left'=>'0']]
]);?>
<?php }?>
</body>
</html>
yii

相關文章
相關標籤/搜索