ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 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> ``` ```