thinkphp框架怎么做一个三级菜单

 我来答
百度网友206b068
2017-11-03 · TA获得超过1117个赞
知道小有建树答主
回答量:1729
采纳率:84%
帮助的人:116万
展开全部
thinkphp框架onethink后台支持三级菜单
onethink后台支持三级菜单,CMS管理后台1.0版本本身不支持三级菜单,稍加改动,即可支持无限级分类
/**
* 获取控制器菜单数组,二级菜单元素位于一级菜单的'_child'元素中
* @author 朱亚杰 <xcoolcc@gmail.com>
*/
final public function getMenus($controller=CONTROLLER_NAME){
// $menus = session('ADMIN_MENU_LIST'.$controller);

$flag=false;//是否开启menu三级菜单

if(empty($menus)){
// 获取主菜单
$where['pid'] = 0;
$where['hide'] = 0;
if(!C('DEVELOP_MODE')){ // 是否开发者模式
$where['is_dev'] = 0;
}
$menus['main'] = M('Menu')->where($where)->order('sort asc')->select();

$menus['child'] = array(); //设置子节点

//高亮主菜单
$current = M('Menu')->where("url like '%{$controller}/".ACTION_NAME."%'")->field('id')->find();
if($current){
$nav = D('Menu')->getPath($current['id']);
$nav_first_title = $nav[0]['title'];

foreach ($menus['main'] as $key => $item) {
if (!is_array($item) || empty($item['title']) || empty($item['url']) ) {
$this->error('控制器基类$menus属性元素配置有误');
}
if( stripos($item['url'],MODULE_NAME)!==0 ){
$item['url'] = MODULE_NAME.'/'.$item['url'];
}
// 判断主菜单权限
if ( !IS_ROOT && !$this->checkRule($item['url'],AuthRuleModel::RULE_MAIN,null) ) {
unset($menus['main'][$key]);
continue;//继续循环
}

// 获取当前主菜单的子菜单项
if($item['title'] == $nav_first_title){
$menus['main'][$key]['class']='current';
//生成child树
$groups = M('Menu')->where("pid = {$item['id']}")->distinct(true)->field("`group`")->select();
if($groups){
$groups = array_column($groups, 'group');
}else{
$groups = array();
}

//获取二级分类的合法url
$where = array();
$where['pid'] = $item['id'];
$where['hide'] = 0;
if(!C('DEVELOP_MODE')){ // 是否开发者模式
$where['is_dev'] = 0;
}
$second_urls = M('Menu')->where($where)->getField('id,url');
$third_urlss=array();

if(!empty($second_urls) && $flag){
foreach($second_urls as $key=>$v){

$wheres = array();
$wheres['pid'] = $key;
$wheres['hide'] = 0;
if(!C('DEVELOP_MODE')){ // 是否开发者模式
$wheres['is_dev'] = 0;
}
$third_urls[] = M('Menu')->where($wheres)->getField('id,url');
foreach($third_urls as $vv){
foreach($vv as $key=>$vs){
$third_urlss[$key]=$vs;
}
}
}
}

if(!IS_ROOT){
// 检测菜单权限
$to_check_urls = array();

foreach ($second_urls as $key=>$to_check_url) {
if( stripos($to_check_url,MODULE_NAME)!==0 ){
$rule = MODULE_NAME.'/'.$to_check_url;
}else{
$rule = $to_check_url;
}
if($this->checkRule($rule, AuthRuleModel::RULE_URL,null))
$to_check_urls[] = $to_check_url;
}
if(!empty($third_urlss) && $flag){
foreach ($third_urlss as $key=>$to_check_urlss) {
if( stripos($to_check_urlss,MODULE_NAME)!==0 ){
$rules = MODULE_NAME.'/'.$to_check_urlss;
}else{
$rules = $to_check_urlss;
}
if($this->checkRule($rules, AuthRuleModel::RULE_URL,null))

echo $rules."<br/>";
$to_check_urlsss[] = $to_check_urlss;
}
}

}

// 按照分组生成子菜单树
foreach ($groups as $g) {
$map = array('group'=>$g);
if(isset($to_check_urls)){
if(empty($to_check_urls)){
// 没有任何权限
continue;
}else{
$map['url'] = array('in', $to_check_urls);
}
}
$map['pid'] = $item['id'];
$map['hide'] = 0;
if(!C('DEVELOP_MODE')){ // 是否开发者模式
$map['is_dev'] = 0;
}
$menuList = M('Menu')->where($map)->field('id,pid,title,url,tip')->order('sort asc')->select();

$menus['child'][$g] = list_to_tree($menuList, 'id', 'pid', 'operater', $item['id']);
//支持三级菜单
if(!empty($menus['child'][$g]) && $flag){
foreach($menuList as $key=>$v){
$maps['pid'] = $v['id'];
$menuLists = M('Menu')->where($maps)->field('id,pid,title,url,tip')->order('sort asc')->select();
$menus['child'][$g][$key]['ttt'] = list_to_tree($menuLists, 'id', 'pid', 'operater', $v['id']);
}
}
}
if($menus['child'] === array()){
//$this->error('主菜单下缺少子菜单,请去系统=》后台菜单管理里添加');
}
}
}
}
// session('ADMIN_MENU_LIST'.$controller,$menus);
}
return $menus;
}
然后Public/base.html
<!-- 边栏 -->
<div class="sidebar">
<!-- 子导航 -->
<block name="sidebar">
<div class="sidebar-collapse" id="sidebar-collapse">
<i class="fa fa-angle-double-left"><<</i>
</div>
<div id="subnav" class="subnav">
<notempty name="_extra_menu">
{// 动态扩展菜单 //}
{:extra_menu($_extra_menu,$__MENU__)}
</notempty>
<volist name="__MENU__.child" id="sub_menu">
<!-- 子导航 -->
<notempty name="sub_menu">
<notempty name="key"><h3><i class="icon icon-unfold"></i>{$key}</h3></notempty>
<ul class="side-sub-menu">
<volist name="sub_menu" id="menu">
<li>
<a class="item" href="{$menu.url|U}">{$menu.title}</a>
</li>
</volist>
<notempty name="menu.ttt">
<volist name="menu.ttt" id="menus">
<li>
<a class="item" href="{$menus.url|U}">{$menus.title}</a>
</li>
</volist>
</notempty>
</ul>
</notempty>
<!-- /子导航 -->
</volist>
</div>
</block>
<!-- /子导航 -->
</div>
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式