服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - tp5框架前台无限极导航菜单类实现方法分析

tp5框架前台无限极导航菜单类实现方法分析

2020-06-03 13:48pan_yuyuan PHP教程

这篇文章主要介绍了tp5框架前台无限极导航菜单类实现方法,结合实例形式分析了tp5框架前台无限极导航菜单类实现原理、步骤与操作注意事项,需要的朋友可以参考下

本文实例讲述了tp5框架前台无限极导航菜单类实现方法。分享给大家供大家参考,具体如下:

适用于 id name pid sort 类似结构的表结构

使用方法:(tp5)

1、将最下面的代码保存到“前台”控制器目录下(名为 FrontNav.php),比如(路径): application/index/controll(应用/模块/控制器)

2、在控制器中使用:(application/index/controll/index)(应用/模块/控制器/方法)

也可以放到基础类的初始化方法中,如:Base.php 的 _initialize() 方法(不用多解释,这个是 tp5 的初始化方法 貌似 init() 也行?可以自己试试)

使用:

1)、第一步:先实例化本类, 5 个参数。

参数说明:

  • param 1:必填 字符串类型 数据表名称(也是模型名称),不用其实字母大写也行。例如: category
  • param 2:选填 字符串类型 模型所在的路径(默认是:admin模块下的model目录)。如果你不叫 admin,那么书写格式如下:houtai/model
  • param 3:必填 字符串类型 父级栏目字段名称,例如:pid(parent id)
  • param 4:选填 数组类型 默认是按 id 正序排序的,如果有排序字段 sortField 的值为 字段名称 如 sort 或者 listorder 等…,sortOrder 的值为 asc(正序) 或 desc (倒序),建议按这个排序,要不然会显示有点乱,因为权重的关系需要手动排序显示的位置。
  • param 5:必填 二维数组 替换关键词,该参数的第一个数组为顶部导航所需要替换的关键词(必填),linkUrl(url 链接)是固定模式,必须这么写,它的值是:模块/控制器/方法,其他的键为要替换的关键词值为字段名称。第二个数组(选填)为二级菜单,第三个数组(选填)为N级菜单,此三个数组个数要对应 $this->createNavHtml() 方法中模版参数的个数,详见 createNavHtml() 方法解释。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$frontNav = new FrontNav('category', '', 'pid', array(
'sortField' => 'sort',
'sortOrder' => 'asc'
), array(
array(
'linkUrl' => 'index/artlist/index',
'catName' => 'name',
'catDesc' => 'desc'
),
array(
'linkUrl' => 'index/artlist/index',
'catName' => 'name',
'catDesc' => 'desc'
)
));

2)、第二步:生成 导航的 html 结构,4个参数

  1. param 1:选填 字符串类型 首页的 html 模版,例如 ‘<li><a class=”navi_home” href=”/”>首页</a></li>'
  2. param 2:必填 数组类型 顶部导航的 html 模版,注意下面实例的格式写法
  3. param 3:选填 数组类型 二级菜单的 html 模版,同上
  4. param 4:选填 数组类型 N级菜单的 html 模版,同上
  1. $navHtml = $frontNav->createNavHtml('<li><a class="navi_home" href="/" rel="external nofollow" rel="external nofollow" >首页</a></li>', array( 
  2. '<ul id="jsddm" class="topNav">'
  3. '<li><a href="linkUrl" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="catDesc">catName</a>'
  4. '</li>'
  5. '</ul>' 
  6. ), array( 
  7. '<ul class="twoLevel">'
  8. '<li><a href="linkUrl" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="catDesc">catName</a>'
  9. '</li>'
  10. '</ul>' 
  11. ), ''); 

3)、第三步:向模版输出

?
1
2
3
$this->assign(array(
'navHtml' => $navHtml
));

4)、第四步:模版调用(多余??)

?
1
2
3
<div id="navi">
{$navHtml}
</div>

提示:

1、替换关键词参数个数与模版(除了首页外)参数个数一定要对应,打字解释的可能有点不明白,详细的对照 实例化 和 创键方法 的代码看几遍就明白了,实在不行可以看源程序,都有较详细的注释。

2、本类默认模型优先,如果没有模型就会查表返回数据库实例。

3、还有一点要注意就是你的替换关键词尽量要跟模版里的字符串不要重复,比如说,你的替换关键词叫 ‘id' => catename,而模版里 <li id=”xixixi”><a href=”###”>哎呀?</a></li>,要是这样就坏了…

求高手改成php原生的,可联系qq发给我吗?嘿嘿…

具体哪有不清楚的可以联系我QQ

效果图:(好像也支持无限极菜单)

  1. <?php 
  2.   /** 
  3.    * Created by PhpStorm. 
  4.    * User: Chao Chao 
  5.    * Date: 2017/9/23 
  6.    * Time: 10:18 
  7.    * versions: 1.0.0 
  8.    * url: null 
  9.    * email: 2776332953@qq.com 
  10.    * phone: *** 
  11.    */ 
  12.   namespace appindexcontroller; 
  13.   use thinkDb;    // 引用 Db (数据库链接) 类 
  14.   use thinkUrl;   // 引用 Url ( 创建 url) 类 
  15.   use thinkLoader;  // 引用 Loader ( 加载 ) 类 
  16.   class FrontNav { 
  17.     // 数据库实例 
  18.     protected $db; 
  19.     // 无限极字段名称 
  20.     protected $pidName = ''
  21.     // 排序设置 
  22.     protected $sort = array(); 
  23.     // 一级导航html模版 
  24.     protected $levelOne = array(); 
  25.     // 二级导航html模版 
  26.     protected $levelTwo = array(); 
  27.     // n级导航html模版 
  28.     protected $levelN = array(); 
  29.     // nav html 
  30.     protected $navHtml = ''
  31.     // 替换关键词 
  32.     protected $replaceKeywords = array(); 
  33.     /** 
  34.      * FrontNav constructor.  构造方法用于生成数据实例与配置参数 
  35.      * @param string $name 数据表名称或模型名称 
  36.      * @param string $modelPath 模型所在路径,默认为 admin/model (admin模块下的model目录) 
  37.      * @param string $pidName 无限极分类的字段(如:pid 或 parentid 等) 
  38.      * @param string $sort 要排序的字段名称 
  39.      * @param array $replaceKeywords 定义的替换关键词 
  40.      */ 
  41.     public function __construct($name, $modelPath, $pidName, $sort, $replaceKeywords) { 
  42.       // $name 为必填参数 
  43.       if (empty($name) || !is_string($name)) { 
  44.         throw new hinkException('参数错误 $name(表名称或模型名称),实例化时该参数必须为字符串类型且不能为空!'); 
  45.       } 
  46.       // 模型优先考虑 如果 模型类先存在 就返回 模型实例,否则返回 Db 类实例。 
  47.       // 防止大小写错误,先都转换成小写在将第一个字母大写 如:Category,因为 linux 区分大小写 
  48.       $fileName = ucwords(strtolower($name)); 
  49.       // 一般栏目的模型都在后台,所以这里就写死了地址 '/admin/model/',也可以传参制定位置 
  50.       $modelPath = !empty($modelPath) ? strtolower($modelPath) : 'admin/model'
  51.       if (class_exists('app\' . str_replace('/', '\', $modelPath) . '\' . $fileName)) { 
  52.         $this->db = Loader::model($fileName, 'model'false'admin'); 
  53.       } else { 
  54.         // 不确定在 linux 下数据库名称是否区分大小写,所以都转换成小写。 
  55.         $this->db = Db::name(strtolower($fileName)); 
  56.       } 
  57.       // 无限极父类字段不能为空 
  58.       if (!empty($pidName)) { 
  59.         $this->pidName = $pidName; 
  60.       } else { 
  61.         throw new hinkException('参数错误 $pidName(父栏目id),实例化时字段名称不能为空!'); 
  62.       } 
  63.       // 替换关键词 
  64.       if (empty($replaceKeywords) || !is_array($replaceKeywords)) { 
  65.         throw new hinkException('参数错误 $replaceKeywords(替换关键词),实例化时该参数必须是而为数组类型且不能为空!');; 
  66.       } else { 
  67.         $this->replaceKeywords = $replaceKeywords; 
  68.       } 
  69.       $this->sort = $sort; 
  70.     } 
  71.     /** 
  72.  * 控制器调用,生成导航菜单。顶层导航的样式( 参数2 $levelOneTemplate )为必填项,也就是说最基本的是一层导航,二级和多级是选填项( 参数3: $levelTwoTemplate 与 参数4 $levelNTemplate 非必填项 ) 
  73.      * @param string $homePageHml 首页 标签的html样式,如: <li><a class="navi_home" href="/" rel="external nofollow" rel="external nofollow" >首页</a></li> 
  74.      * @param array $levelOneTemplate 必填 顶部导航的html样式,如: array( 
  75.      * '<ul id="jsddm" class="topNav">',  最外层 ul 
  76.      * '<li><a href="linkUrl" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="catDesc">catName</a>',  li标签 
  77.      * '</li>',  li 结束 
  78.      * '</ul>' ul 结束 
  79.      * ) 
  80.      * @param array $levelTwoTemplate 选填 二级菜单的html样式,如: array( 
  81.      * '<ul class="twoLevel">',  二级菜单的 ul 
  82.      * '<li><a href="linkUrl" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="catDesc">catName</a>',  li标签 
  83.      * '</li>',li 结束 
  84.      * '</ul>'ul 结束 
  85.      * ) 
  86.      * @param array $levelNTemplate 选填 多级菜单的html样式,如: array( 
  87.      * '<ul class="nLevel">',  N级菜单的 ul 
  88.      * '<li><a href="linkUrl" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="catDesc">catName</a>',  li标签 
  89.      * '</li>',li 结束 
  90.      * '</ul>'ul 结束 
  91.      * @return string 
  92.      */ 
  93.     public function createNavHtml($homePageHml, $levelOneTemplate, $levelTwoTemplate, $levelNTemplate) { 
  94.       // 第一层导航不能为空且必须是数组 
  95.       if (empty($levelOneTemplate) || !is_array($levelOneTemplate)) { 
  96.         throw new hinkException('参数错误 $levelOneTemplate(一级导航模版),该参数必须是数组类型且不能为空!'); 
  97.       } 
  98.       $this->levelOne = $levelOneTemplate; 
  99.       // 二级导航 
  100.       if (!empty($levelTwoTemplate) && !is_array($levelTwoTemplate)) { 
  101.         throw new hinkException('参数错误 $levelTwoTemplate(二级导航模版),该参数可以为空 '' 或 array(),否则必须是数组类型!'); 
  102.       } 
  103.       $this->levelTwo = $levelTwoTemplate; 
  104.       // N级导航 
  105.       if (!empty($levelNTemplate) && !is_array($levelNTemplate)) { 
  106.         throw new hinkException('参数错误 $levelNTemplate(N级导航模版),该参数可以为空 '' 或 array(),否则必须是数组类型!'); 
  107.       } 
  108.       $this->levelN = $levelNTemplate; 
  109.       $treeData = $this->getTreeData($this->getAllData(), 0); 
  110.       //print_r($treeData); 
  111.       $this->createHtml($treeData); 
  112.       return $this->levelOne[0] . (!empty($homePageHml) ? $homePageHml : '') . $this->navHtml . 
  113.           $this->levelOne[3] . " "
  114.     } 
  115.     /** 
  116.      * 获取所有数据 
  117.      * @return array 
  118.      */ 
  119.     private function getAllData() { 
  120.     if (empty($this->sort) || empty($this->sort['sortField']) || empty($this->sort['sortOrder'])) { 
  121.         return collection($this->db->where(1) 
  122.                       ->select())->toArray(); 
  123.       } else { 
  124.         return collection($this->db->where(1) 
  125.                       ->order($this->sort['sortField'] . ' ' . $this->sort['sortOrder']) 
  126.                       ->select())->toArray(); 
  127.       } 
  128.     } 
  129.     /** 
  130.      * 将所有数据攒成树状结构的数组 
  131.      * 增加 levels (层级) children (子数组) 
  132.      * @param $allData   传递过来的所有非树状结构的数组 
  133.      * @param $parentId   初始化时的父栏目id 
  134.      * @return array    树状结构的数组 
  135.      */ 
  136.     private function getTreeData($allData, $parentId) { 
  137.       $tree = array(); 
  138.       // 层级计数 
  139.       static $number = 1; 
  140.       foreach ($allData as $v) { 
  141.         if ($v[$this->pidName] == $parentId) { 
  142.           if ($v[$this->pidName] == 0) { 
  143.             $v['levels'] = 0; 
  144.           } else { 
  145.             $v['levels'] = $number; 
  146.             ++$number; 
  147.           } 
  148.           $v['children'] = $this->getTreeData($allData, $v['id']); 
  149.           $tree[] = $v; 
  150.         } else { 
  151.           $number = 1; 
  152.         } 
  153.       } 
  154.       return $tree; 
  155.     } 
  156.     /** 
  157.      * 递归生成树状结构的html 
  158.      * @param $allData array  由 createNavHtml() 方法传递过来的 树形结构 数据(数组) 
  159.      * @return     string 返回(最外层ul内部的html)树状结构的html 
  160.      */ 
  161.     private function createHtml($allData) { 
  162.       foreach ($allData as $v) { 
  163.         // 顶部导航 
  164.         if ($v['levels'] == 0) { 
  165.           $tempStr0 = $this->levelOne[1]; 
  166.           foreach ($this->replaceKeywords[0] as $k1 => $v1) { 
  167.             if ($k1 == 'linkUrl') { 
  168.               $tempStr0 = str_replace($k1, Url::build($v1, 'id=' . $v['id']), " " . $tempStr0); 
  169.             } else { 
  170.               $tempStr0 = str_replace($k1, $v[$v1], $tempStr0); 
  171.             } 
  172.           } 
  173.           $this->navHtml .= $tempStr0; 
  174.           if (empty($v['children'])) { 
  175.             $this->navHtml .= $this->levelOne[2] . " "
  176.           } else if (!empty($v['children']) && !empty($this->levelTwo)) { 
  177.             $this->navHtml .= " " . $this->levelTwo[0] . " "
  178.             $this->createHtml($v['children']); 
  179.             $this->navHtml .= $this->levelTwo[3] . $this->levelOne[2]; 
  180.           } 
  181.         } 
  182.         // 二级菜单 
  183.         if ($v['levels'] == 1) { 
  184.           $tempStr2 = $this->levelTwo[1]; 
  185.           foreach ($this->replaceKeywords[1] as $k1 => $v1) { 
  186.             if ($k1 == 'linkUrl') { 
  187.               $tempStr2 = str_replace($k1, Url::build($v1, 'id=' . $v['id']),       $tempStr2); 
  188.             } else { 
  189.               $tempStr2 = str_replace($k1, $v[$v1], $tempStr2); 
  190.             } 
  191.           } 
  192.           $this->navHtml .= $tempStr2; 
  193.           if (empty($v['children'])) { 
  194.             $this->navHtml .= $this->levelTwo[2] . " "
  195.           } else if (!empty($v['children']) && !empty($this->levelN)) { 
  196.             // 是否多级导航,有 children ,还必须有3级 html 模版 
  197.             $this->navHtml .= " " . $this->levelN[0] . " "
  198.             $this->createHtml($v['children']); 
  199.             $this->navHtml .= $this->levelN[3] . $this->levelTwo[2] . " "
  200.           } 
  201.         } 
  202.         // 多级菜单 
  203.         if (!empty($this->levelN) && $v['levels'] > 1) { 
  204.           $tempStrN = $this->levelN[1]; 
  205.           foreach ($this->replaceKeywords[2] as $k1 => $v1) { 
  206.             if ($k1 == 'linkUrl') { 
  207.               $tempStrN = str_replace($k1, Url::build($v1, 'id=' . $v['id']), $tempStrN); 
  208.             } else { 
  209.               $tempStrN = str_replace($k1, $v[$v1], $tempStrN); 
  210.             } 
  211.           } 
  212.           $this->navHtml .= $tempStrN; 
  213.           if (empty($v['children'])) { 
  214.             $this->navHtml .= $this->levelN[2] . " "
  215.           } else { 
  216.             $this->navHtml .= $this->levelN[0]; 
  217.             $this->createHtml($v['children']); 
  218.             $this->navHtml .= $this->levelN[3] . $this->levelN[2]; 
  219.           } 
  220.         } 
  221.       } 
  222.       return $this->navHtml; 
  223.     } 
  224.   } 
 

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

原文链接:https://blog.csdn.net/pan_yuyuan/article/details/81844194

延伸 · 阅读

精彩推荐