# 8. 创建菜单
## 创建后台菜单
菜单数据以数组形式定义,定义好之后可以通`create_menu`函数手动创建菜单,通过`del_menu`函数来手动删除菜单。当然,除了以上方式还支持系统自动判断导入菜单,请参考下面的自动导入方式。
## 字段说明
> 2.1.220220版本之前,字段需要填写完整
字段名说明默认值是否必填parent\_id父菜单ID或父菜单的name,默认0,表示最顶级0-title菜单名称,菜单多语言目前需要在admin\_route后台路由事件中手动导入菜单语言包-必填name权限规则-必填route路由-icon图标fas fa-align-ustify/far fa-circle-remark备注-weigh排序0-type类型:0-权限规则,1-菜单,2-菜单头(提供的额外标识)1-child子级菜单,有子级的话按一级菜单字段填写即可--1. name,权限规则
写法:控制器/方法,二级文件夹则是文件夹名/控制器/方法,注意二级需要填写路由
2. route,路由地址
写法:文件夹名.控制器/方法,常用于有文件夹的情况,注意是“点”
3. icon,图标
基于`fontawesome`,一级菜单默认为`fas fa-align-justify`,二级菜单默认`far fa-circle` 图标地址:[https://fa5.dashgame.com/#/图标](https://fa5.dashgame.com/#/%E5%9B%BE%E6%A0%87)
4. type,菜单类型
未填写`type`时系统会自动判断菜单类型,在认为有子级情况下认为是菜单。若有特殊情况,您需要填写`type`字段
## 自动导入示例
在插件名称类里面增加公共属性`$menu`,在安装时检测到有这个属性就会自动导入,另外在`卸载`、`启用`、`禁用`、`更新`等操作时系统会自动对菜单进行相应的操作,例如插件禁用,菜单也会被禁用。
> 注意:如果菜单已存在并且这个菜单不属于该插件就不会导入,如果已存在,属于该插件则是更新菜单。
> 插件升级操作时,同样会根据menu导入,如果存在则是更新该菜单,如有额外处理,可在upgrade方法中操作
```
<pre class="calibre16">```
<span class="token"><</span><span class="token">?</span>php
declare <span class="token1">(</span>strict_types<span class="token">=</span><span class="token3">1</span><span class="token1">)</span><span class="token1">;</span>
namespace addons\demo<span class="token1">;</span>
use think\Addons<span class="token1">;</span>
class <span class="token6">Demo</span> extends <span class="token6">Addons</span>
<span class="token1">{</span>
<span class="token5">// 菜单</span>
public $menu <span class="token">=</span> <span class="token1">[</span>
<span class="token1">[</span>
<span class="token4">'title'</span><span class="token">=</span><span class="token">></span><span class="token4">'插件测试'</span><span class="token1">,</span>
<span class="token4">'name'</span><span class="token">=</span><span class="token">></span><span class="token4">'demo'</span><span class="token1">,</span>
<span class="token4">'child'</span><span class="token">=</span><span class="token">></span><span class="token1">[</span>
<span class="token1">[</span><span class="token4">'title'</span><span class="token">=</span><span class="token">></span><span class="token4">'查看'</span><span class="token1">,</span><span class="token4">'name'</span><span class="token">=</span><span class="token">></span><span class="token4">'demo/index'</span><span class="token1">]</span><span class="token1">,</span>
<span class="token1">[</span><span class="token4">'title'</span><span class="token">=</span><span class="token">></span><span class="token4">'添加'</span><span class="token1">,</span><span class="token4">'name'</span><span class="token">=</span><span class="token">></span><span class="token4">'demo/add'</span><span class="token1">]</span><span class="token1">,</span>
<span class="token1">[</span><span class="token4">'title'</span><span class="token">=</span><span class="token">></span><span class="token4">'修改'</span><span class="token1">,</span><span class="token4">'name'</span><span class="token">=</span><span class="token">></span><span class="token4">'demo/edit'</span><span class="token1">]</span><span class="token1">,</span>
<span class="token1">[</span><span class="token4">'title'</span><span class="token">=</span><span class="token">></span><span class="token4">'删除'</span><span class="token1">,</span><span class="token4">'name'</span><span class="token">=</span><span class="token">></span><span class="token4">'demo/del'</span><span class="token1">]</span><span class="token1">,</span>
<span class="token1">]</span>
<span class="token1">]</span>
<span class="token1">]</span><span class="token1">;</span>
public <span class="token2">function</span> <span class="token6">install</span><span class="token1">(</span><span class="token1">)</span>
<span class="token1">{</span>
<span class="token2">return</span> <span class="token3">true</span><span class="token1">;</span>
<span class="token1">}</span>
public <span class="token2">function</span> <span class="token6">uninstall</span><span class="token1">(</span><span class="token1">)</span>
<span class="token1">{</span>
<span class="token2">return</span> <span class="token3">true</span><span class="token1">;</span>
<span class="token1">}</span>
<span class="token1">}</span>
```
```
## 菜单多语言
在插件目录,在data目录下创建zh-cn.php文件,如下图
![](https://img.kancloud.cn/a1/45/a14511db706c16ac94ad71d4e4739bdc_212x111.png)
目前后台内置的中英语言包、因此在上面讲的$menu变量里面title全部写成英文,然后下面的写上英文跟中文的对照。
```
<pre class="calibre16">```
<span class="token"><</span><span class="token">?</span>php
<span class="token2">return</span> <span class="token1">[</span>
<span class="token4">'Addon test'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'插件測試'</span>
<span class="token1">]</span><span class="token1">;</span>
```
```
支持其他语言包写法,例如繁体中文命名为:zh-tw.php,那么代码如下:
```
<pre class="calibre16">```
<span class="token"><</span><span class="token">?</span>php
<span class="token2">return</span> <span class="token1">[</span>
<span class="token4">'Addon test'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'插件測試'</span>
<span class="token1">]</span><span class="token1">;</span>
```
```
创建文件后我们需要在插件初始化时加载语言包
```
<pre class="calibre16">```
public <span class="token2">function</span> <span class="token6">addonsInitHook</span><span class="token1">(</span><span class="token1">)</span>
<span class="token1">{</span>
<span class="token5">// 获取语言包对象</span>
$lang <span class="token">=</span> <span class="token6">app</span><span class="token1">(</span><span class="token1">)</span><span class="token">-</span><span class="token">></span>lang<span class="token1">;</span>
<span class="token5">// 加载语言包,$this->addon_path 插件目录,DIRECTORY_SEPARATOR 是 '/' 或 '\',$lang->getLangset() 是当前语言</span>
$lang<span class="token">-</span><span class="token">></span><span class="token6">load</span><span class="token1">(</span>$this<span class="token">-</span><span class="token">></span>addon_path<span class="token1">.</span><span class="token4">'data'</span><span class="token1">.</span>DIRECTORY_SEPARATOR<span class="token1">.</span>$lang<span class="token">-</span><span class="token">></span><span class="token6">getLangset</span><span class="token1">(</span><span class="token1">)</span><span class="token1">.</span><span class="token4">'.php'</span><span class="token1">)</span><span class="token1">;</span>
<span class="token1">}</span>
```
```
## 手动导入
如下,我们可以创建PHP文件,放到插件的data目录下,如下
![](https://img.kancloud.cn/32/5c/325c9630822f033e706715b5654217aa_294x148.png)
```
<pre class="calibre16">```
<span class="token"><</span><span class="token">?</span>php
<span class="token2">return</span> <span class="token1">[</span>
<span class="token1">[</span>
<span class="token5">// 父菜单ID或父菜单name或0</span>
<span class="token4">"parent_id"</span> <span class="token">=</span><span class="token">></span> <span class="token3">57</span><span class="token1">,</span>
<span class="token5">// 标题</span>
<span class="token4">"title"</span> <span class="token">=</span><span class="token">></span> <span class="token4">'菜单名称'</span><span class="token1">,</span>
<span class="token5">// 权限认证规则</span>
<span class="token4">"name"</span><span class="token">=</span><span class="token">></span><span class="token4">"createhtml"</span><span class="token1">,</span>
<span class="token5">// 路由地址</span>
<span class="token4">"route"</span> <span class="token">=</span><span class="token">></span> <span class="token4">""</span><span class="token1">,</span>
<span class="token5">// fontawesome 图标</span>
<span class="token4">"icon"</span> <span class="token">=</span><span class="token">></span> <span class="token4">'fas fa-print'</span><span class="token1">,</span>
<span class="token5">// 备注</span>
<span class="token4">"remark"</span> <span class="token">=</span><span class="token">></span> <span class="token4">""</span><span class="token1">,</span>
<span class="token5">// 排序</span>
<span class="token4">"weigh"</span> <span class="token">=</span><span class="token">></span> <span class="token3">10</span><span class="token1">,</span>
<span class="token5">// 菜单类型</span>
<span class="token4">"type"</span> <span class="token">=</span><span class="token">></span> <span class="token3">1</span><span class="token1">,</span>
<span class="token5">// 子级</span>
<span class="token4">"child"</span><span class="token">=</span><span class="token">></span><span class="token1">[</span>
<span class="token1">[</span>
<span class="token5">// 标题</span>
<span class="token4">"title"</span> <span class="token">=</span><span class="token">></span> <span class="token4">'菜单名称1'</span><span class="token1">,</span>
<span class="token5">// 权限认证规则</span>
<span class="token4">"name"</span><span class="token">=</span><span class="token">></span><span class="token4">"createhtml/index1"</span><span class="token1">,</span>
<span class="token5">// 子级</span>
<span class="token4">"child"</span><span class="token">=</span><span class="token">></span><span class="token1">[</span>
<span class="token1">[</span><span class="token4">"title"</span> <span class="token">=</span><span class="token">></span> <span class="token4">'菜单名称2'</span><span class="token1">,</span><span class="token4">"name"</span><span class="token">=</span><span class="token">></span><span class="token4">"createhtml/index"</span><span class="token1">,</span><span class="token1">]</span>
<span class="token1">]</span>
<span class="token1">]</span><span class="token1">,</span>
<span class="token1">[</span>
<span class="token5">// 标题</span>
<span class="token4">"title"</span> <span class="token">=</span><span class="token">></span> <span class="token4">'菜单名称3'</span><span class="token1">,</span>
<span class="token5">// 权限认证规则</span>
<span class="token4">"name"</span><span class="token">=</span><span class="token">></span><span class="token4">"createhtml/index"</span><span class="token1">,</span>
<span class="token1">]</span>
<span class="token1">]</span>
<span class="token1">]</span><span class="token1">,</span>
<span class="token1">]</span><span class="token1">;</span>
```
```
在安装的时候导入,使用`create_menu`方法导入。导入后无需管禁用、启用与卸载的操作了,系统会自动去操作菜单。
```
<pre class="calibre16">```
public <span class="token2">function</span> <span class="token6">install</span><span class="token1">(</span><span class="token1">)</span>
<span class="token1">{</span>
<span class="token5">// 菜单数组文件路径</span>
$menu <span class="token">=</span> include $this<span class="token">-</span><span class="token">></span>addon_path<span class="token1">.</span><span class="token4">'data'</span><span class="token1">.</span>DIRECTORY_SEPARATOR<span class="token1">.</span><span class="token4">'menu.php'</span><span class="token1">;</span>
<span class="token5">// 参数一给路径,参数二给当前插件的标识</span>
<span class="token6">create_menu</span><span class="token1">(</span>$menu<span class="token1">,</span>$this<span class="token">-</span><span class="token">></span><span class="token6">getName</span><span class="token1">(</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span>
<span class="token2">return</span> <span class="token3">true</span><span class="token1">;</span>
<span class="token1">}</span>
```
```
## 方法的使用
- create\_menu 创建菜单
参数1为菜单数组,参数2为菜单属于的插件标识。
> 注意:如果菜单已存在并且这个菜单不属于该插件就不会导入,如果已存在,属于该插件则是更新菜单
```
<pre class="calibre16">```
<span class="token5">// 在插件里面可以直接获取到插件标识,当然你也可以直接写上你的插件标识</span>
<span class="token6">create_menu</span><span class="token1">(</span>$menu<span class="token1">,</span> $this<span class="token">-</span><span class="token">></span><span class="token6">getName</span><span class="token1">(</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span>
```
```
- status\_menu 更改状态:启用与禁用.
参数1为:1=启用,0=禁用,参数2为插件名称标识
```
<pre class="calibre16">```
<span class="token6">status_menu</span><span class="token1">(</span><span class="token3">1</span><span class="token1">,</span> $this<span class="token">-</span><span class="token">></span><span class="token6">getName</span><span class="token1">(</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> <span class="token5">// 启用菜单</span>
```
```
```
<pre class="calibre16">```
<span class="token5">// 例如插件叫demo</span>
<span class="token6">status_menu</span><span class="token1">(</span><span class="token3">0</span><span class="token1">,</span> <span class="token4">'demo'</span><span class="token1">)</span><span class="token1">;</span> <span class="token5">// 禁用demo的菜单</span>
```
```
- del\_menu 删除菜单
```
<pre class="calibre20">```
<span class="token5">// 参数1为插件标识</span>
<span class="token6">del_menu</span><span class="token1">(</span>$name<span class="token1">)</span><span class="token1">;</span>
```
```
- 简介
- 下载与安装配置
- 下载
- 虚拟主机安装
- 百度云虚拟主机
- 创业云虚拟主机
- Linux下宝塔安装
- 宝塔一键部署
- Windows下宝塔安装
- phpEnv下安装
- phpStudy下安装
- 伪静态(url重写)
- 安装系统
- 调试模式与env文件
- 重新安装与安装文件说明
- 后台使用帮助
- 后台入口文件
- 首页面板
- 配置中心
- 站点配置
- SEO设置
- SEO设置-静态生成
- 文档属性
- 站点模块
- V2.2.2版本 -
- V2.2.3版本 +
- 模型管理
- 栏目管理
- 字段类型与组件
- 前言
- 前端组件
- 验证规则
- text 单行文本
- textarea 多行文本
- radio 单选
- checkbox 复选
- select 下拉列表
- selects下拉列表(多)
- selectpage 动态下拉
- editor编辑器
- image图片
- images图片(多)
- number数字
- date日期
- datetime日期和时间
- downfile文件上传
- downfiles文件上传(多)
- array 数组(键值对)
- 内容管理
- 自定义URL
- 多语言
- 界面多语言
- 内容多语言
- 多语言URL地址
- 附件管理
- 应用中心
- 标签语法
- 标签使用必读
- 列表标签
- content 内容列表标签
- contentpage 内容列表分页
- wfpage 瀑布流分页
- filter 筛选标签
- order 排序标签
- 内容标签
- guestbook 留言(表单)标签(废弃)
- form 表单标签
- pre 上一篇标签
- next 下一篇标签
- prenext 上一篇下一篇(高级)标签
- fileinfo 获取附件信息(文件下载)
- 内容分页
- raw 格式化编辑器文本(即将废弃)
- 全局标签
- seo 标签
- channel 栏目标签
- arcone 获取单条文档
- breadcrumb 面包屑导航标签
- adv 站点模块标签
- date 日期格式化标签
- substr字符(标题)截取标签
- volist 循环标签
- language 获取语言列表标签
- query 查询数据库内容
- include 导入模板文件标签
- 原生PHP与php标签
- map生成位置地图
- thumb 生成缩略图
- addons 插件检测
- 标签管理
- taglist标签(获取标签列表)
- tagarclist根据标签获取文章内容列表
- 导入标签
- 模板开发
- 1. 创建模板
- 2. 模板目录结构
- 3. 模板描述文件(重要) info.ini
- 4. 变量输出格式
- 4. 模板变量
- 5. 获取站点配置
- 6. 模板界面多语言
- 7. 模板静态目录及调用静态文件
- 8. 模板定义插件事件
- 9. 模板搜索页
- 10. 模板配置JSON
- 11. 导入演示数据
- 12. 卸载删除演示数据
- 13. 模板文件打包
- 14. 整站源码打包
- 插件开发
- 1. 创建插件
- 2. 目录结构
- 3. 插件名称类
- 4. 插件信息(info.ini)
- 5. 插件事件
- 6. 配置文件
- 7. install 安装目录
- 8. 创建菜单
- 9. 插件控制器
- 10. 插件视图
- 10-1 插件主题化
- 11. 插件模型
- 12. 模型事件
- 13. 插件内置函数
- 13-1 插件函数库
- 14. 插件多语言
- 15. 插件中间件
- 16. 插件路由
- 17. 插件验证器
- 18. 标签库
- 19. 第三方类库
- 20. 数据库文件导入
- 插件打包
- 后台前端使用帮助
- 二次开发帮助(更新中)
- 后台前端开发帮助
- 路由(链接地址、访问规则)
- URL访问
- 路由定义
- URL生成
- 附录
- 数据字典
- 助手函数库