企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 思路 比较简单 一:提交验证器验证 二:插入到数据表 `admin_menu` 三:插入到 `auth_rule` 表(可选) 四:导出到 `admin_menu.php` 语言包!(可选) ## 代码 >特别注意下边的注释代码 >`注释一`的代码是为了写入到 `auth_rule` 表当中的,如果想写入的话,这里可以把注释去掉,如果不用可以删除! >`注释二` 的代码是为了导出语言包的!如果不用可以删除!导出需要方法 `_exportAppMenuDefaultLang`! ``` <?php namespace app\agent\controller; use think\Controller; use think\Db; class MenuController extends Controller { /** * 后台菜单添加提交保存 */ public function addPost() { if ($this->request->isPost()) { $result = $this->validate($this->request->param(), 'AdminMenu'); if ($result !== true) { $this->error($result); } else { $data = $this->request->param(); Db::name('AdminMenu')->strict(false)->field(true)->insert($data); // 注释一:这里写入到 auth_rule 表 // $app = $this->request->param("app"); // $controller = $this->request->param("controller"); // $action = $this->request->param("action"); // $param = $this->request->param("param"); // $authRuleName = "$app/$controller/$action"; // $menuName = $this->request->param("name"); // $findAuthRuleCount = Db::name('AuthRule')->where([ // 'app' => $app, // 'name' => $authRuleName, // 'type' => 'admin_url' // ])->count(); // if (empty($findAuthRuleCount)) { // Db::name('AuthRule')->insert([ // "name" => $authRuleName, // "app" => $app, // "type" => "admin_url", // "title" => $menuName, // 'param' => $param, // ]); // } $sessionAdminMenuIndex = session('admin_menu_index'); $to = empty($sessionAdminMenuIndex) ? "Menu/index" : $sessionAdminMenuIndex; // 注释二:这里导出表 // $this->_exportAppMenuDefaultLang(); $this->success("添加成功!", url($to)); } } } } ``` ## 导出后台菜单语言包方法 ``` /** * 导出后台菜单语言包 */ private function _exportAppMenuDefaultLang() { $menus = Db::name('AdminMenu')->order(["app" => "ASC", "controller" => "ASC", "action" => "ASC"])->select(); $langDir = config('DEFAULT_LANG'); $adminMenuLang = CMF_ROOT . "data/lang/" . $langDir . "/admin_menu.php"; if (!empty($adminMenuLang) && !file_exists_case($adminMenuLang)) { mkdir(dirname($adminMenuLang), 0777, true); } $lang = []; foreach ($menus as $menu) { $lang_key = strtoupper($menu['app'] . '_' . $menu['controller'] . '_' . $menu['action']); $lang[$lang_key] = $menu['name']; } $langStr = var_export($lang, true); $langStr = preg_replace("/\s+\d+\s=>\s(\n|\r)/", "\n", $langStr); if (!empty($adminMenuLang)) { file_put_contents($adminMenuLang, "<?php\nreturn $langStr;"); } } ``` >问一:那数据导出到哪了? 答:在 /data/lang/zh-cn/admin_menu.php 中 ![mark](http://qiniu.newthink.cc/blog/20171019-121113505.png) >问二:数据表及文件数据格式是什么呢? 答:如图: 1、操作完成列表显示 ![mark](http://qiniu.newthink.cc/blog/20171019-121239199.png) 2、数据库 ![mark](http://qiniu.newthink.cc/blog/20171019-121201703.png) 3、admin_menu.php 文件 ![mark](http://qiniu.newthink.cc/blog/20171019-121224615.png)