## 思路
一:将每一个控制器方法上放一个注释说明
二:循环遍历所有模块儿及下边每一个控制器的方法
二:用 `Annotations` 将每一个方法读出并写入到`auth_rule`
## 代码
```
<?php
namespace app\agent\controller;
use think\Controller;
use think\Db;
use mindplay\annotations\Annotations;
class MenuController extends Controller
{
/**
* agent测试
* @adminMenu(
* 'name' => '钩子管理',
* 'parent' => 'admin/Plugin/default',
* 'display'=> true,
* 'hasView'=> true,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => 'agent测试描述',
* 'param' => ''
* )
*/
public function getActions()
{
Annotations::$config['cache'] = false;
$annotationManager = Annotations::getManager();
$annotationManager->registry['adminMenu'] = 'app\agent\annotation\AdminMenuAnnotation';
$annotationManager->registry['adminMenuRoot'] = 'app\agent\annotation\AdminMenuRootAnnotation';
$newMenus = [];
$apps = cmf_scan_dir(APP_PATH . '*', GLOB_ONLYDIR);
$app = $this->request->param('app', '');
if (empty($app)) {
$app = $apps[0];
}
if (!in_array($app, $apps)) {
$this->error('应用' . $app . '不存在!');
}
if ($app == 'agent') {
$filePatten = APP_PATH . $app . '/controller/*Controller.php';
} else {
$filePatten = APP_PATH . $app . '/controller/Admin*Controller.php';
}
$controllers = cmf_scan_dir($filePatten);
if (!empty($controllers)) {
foreach ($controllers as $controller) {
$controller = preg_replace('/\.php$/', '', $controller);
$controllerName = preg_replace('/\Controller$/', '', $controller);
$controllerClass = "app\\$app\\controller\\$controller";
$menuAnnotations = Annotations::ofClass($controllerClass, '@adminMenuRoot');
if (!empty($menuAnnotations)) {
foreach ($menuAnnotations as $menuAnnotation) {
$name = $menuAnnotation->name;
$icon = $menuAnnotation->icon;
$type = 0;//1:有界面可访问菜单,2:无界面可访问菜单,0:只作为菜单
$action = $menuAnnotation->action;
$status = empty($menuAnnotation->display) ? 0 : 1;
$listOrder = floatval($menuAnnotation->order);
$param = $menuAnnotation->param;
$remark = $menuAnnotation->remark;
if (empty($menuAnnotation->parent)) {
$parentId = 0;
} else {
$parent = explode('/', $menuAnnotation->parent);
$countParent = count($parent);
if ($countParent > 3) {
throw new \Exception($controllerClass . ':' . $action . ' @adminMenuRoot parent格式不正确!');
}
$parentApp = $app;
$parentController = $controllerName;
$parentAction = '';
switch ($countParent) {
case 1:
$parentAction = $parent[0];
break;
case 2:
$parentController = $parent[0];
$parentAction = $parent[1];
break;
case 3:
$parentApp = $parent[0];
$parentController = $parent[1];
$parentAction = $parent[2];
break;
}
$findParentAdminMenu = Db::name('admin_menu')->where([
'app' => $parentApp,
'controller' => $parentController,
'action' => $parentAction
])->find();
if (empty($findParentAdminMenu)) {
$parentId = Db::name('admin_menu')->insertGetId([
'app' => $parentApp,
'controller' => $parentController,
'action' => $parentAction,
'name' => '--new--'
]);
} else {
$parentId = $findParentAdminMenu['id'];
}
}
$findAdminMenu = Db::name('admin_menu')->where([
'app' => $app,
'controller' => $controllerName,
'action' => $action
])->find();
if (empty($findAdminMenu)) {
Db::name('admin_menu')->insert([
'parent_id' => $parentId,
'type' => $type,
'status' => $status,
'list_order' => $listOrder,
'app' => $app,
'controller' => $controllerName,
'action' => $action,
'param' => $param,
'name' => $name,
'icon' => $icon,
'remark' => $remark
]);
$menuName = $name;
array_push($newMenus, "$app/$controllerName/$action 已导入");
} else {
if ($findAdminMenu['name'] == '--new--') {
Db::name('admin_menu')->where([
'app' => $app,
'controller' => $controllerName,
'action' => $action
])->update([
'parent_id' => $parentId,
'type' => $type,
'status' => $status,
'list_order' => $listOrder,
'param' => $param,
'name' => $name,
'icon' => $icon,
'remark' => $remark
]);
$menuName = $name;
} else {
// 只关注菜单层级关系,是否有视图
Db::name('admin_menu')->where([
'app' => $app,
'controller' => $controllerName,
'action' => $action
])->update([
'parent_id' => $parentId,
'type' => $type,
]);
$menuName = $findAdminMenu['name'];
}
array_push($newMenus, "$app/$controllerName/$action 层级关系已更新");
}
$authRuleName = "{$app}/{$controllerName}/{$action}";
$findAuthRuleCount = Db::name('auth_rule')->where([
'app' => $app,
'name' => $authRuleName,
'type' => 'admin_url'
])->count();
if ($findAuthRuleCount == 0) {
Db::name('auth_rule')->insert([
'app' => $app,
'name' => $authRuleName,
'type' => 'admin_url',
'param' => $param,
'title' => $menuName
]);
} else {
Db::name('auth_rule')->where([
'app' => $app,
'name' => $authRuleName,
'type' => 'admin_url',
])->update([
'param' => $param,
'title' => $menuName
]);
}
}
}
$reflect = new \ReflectionClass($controllerClass);
$methods = $reflect->getMethods(\ReflectionMethod::IS_PUBLIC);
if (!empty($methods)) {
foreach ($methods as $method) {
if ($method->class == $controllerClass && strpos($method->name, '_') !== 0) {
$menuAnnotations = Annotations::ofMethod($controllerClass, $method->name, '@adminMenu');
if (!empty($menuAnnotations)) {
$menuAnnotation = $menuAnnotations[0];
$name = $menuAnnotation->name;
$icon = $menuAnnotation->icon;
$type = $menuAnnotation->hasView ? 1 : 2;//1:有界面可访问菜单,2:无界面可访问菜单,0:只作为菜单
$action = $method->name;
$status = empty($menuAnnotation->display) ? 0 : 1;
$listOrder = floatval($menuAnnotation->order);
$param = $menuAnnotation->param;
$remark = $menuAnnotation->remark;
if (empty($menuAnnotation->parent)) {
$parentId = 0;
} else {
$parent = explode('/', $menuAnnotation->parent);
$countParent = count($parent);
if ($countParent > 3) {
throw new \Exception($controllerClass . ':' . $action . ' @menuRoot parent格式不正确!');
}
$parentApp = $app;
$parentController = $controllerName;
$parentAction = '';
switch ($countParent) {
case 1:
$parentAction = $parent[0];
break;
case 2:
$parentController = $parent[0];
$parentAction = $parent[1];
break;
case 3:
$parentApp = $parent[0];
$parentController = $parent[1];
$parentAction = $parent[2];
break;
}
$findParentAdminMenu = Db::name('admin_menu')->where([
'app' => $parentApp,
'controller' => $parentController,
'action' => $parentAction
])->find();
if (empty($findParentAdminMenu)) {
$parentId = Db::name('admin_menu')->insertGetId([
'app' => $parentApp,
'controller' => $parentController,
'action' => $parentAction,
'name' => '--new--'
]);
} else {
$parentId = $findParentAdminMenu['id'];
}
}
$findAdminMenu = Db::name('admin_menu')->where([
'app' => $app,
'controller' => $controllerName,
'action' => $action
])->find();
if (empty($findAdminMenu)) {
Db::name('admin_menu')->insert([
'parent_id' => $parentId,
'type' => $type,
'status' => $status,
'list_order' => $listOrder,
'app' => $app,
'controller' => $controllerName,
'action' => $action,
'param' => $param,
'name' => $name,
'icon' => $icon,
'remark' => $remark
]);
$menuName = $name;
array_push($newMenus, "$app/$controllerName/$action 已导入");
} else {
if ($findAdminMenu['name'] == '--new--') {
Db::name('admin_menu')->where([
'app' => $app,
'controller' => $controllerName,
'action' => $action
])->update([
'parent_id' => $parentId,
'type' => $type,
'status' => $status,
'list_order' => $listOrder,
'param' => $param,
'name' => $name,
'icon' => $icon,
'remark' => $remark
]);
$menuName = $name;
} else {
// 只关注菜单层级关系,是否有视图
Db::name('admin_menu')->where([
'app' => $app,
'controller' => $controllerName,
'action' => $action
])->update([
'parent_id' => $parentId,
'type' => $type,
]);
$menuName = $findAdminMenu['name'];
}
array_push($newMenus, "$app/$controllerName/$action 已更新");
}
$authRuleName = "{$app}/{$controllerName}/{$action}";
$findAuthRuleCount = Db::name('auth_rule')->where([
'app' => $app,
'name' => $authRuleName,
'type' => 'admin_url'
])->count();
if ($findAuthRuleCount == 0) {
Db::name('auth_rule')->insert([
'app' => $app,
'name' => $authRuleName,
'type' => 'admin_url',
'param' => $param,
'title' => $menuName
]);
} else {
Db::name('auth_rule')->where([
'app' => $app,
'name' => $authRuleName,
'type' => 'admin_url',
])->update([
'param' => $param,
'title' => $menuName
]);
}
}
}
}
}
}
}
$index = array_search($app, $apps);
$nextIndex = $index + 1;
$nextIndex = $nextIndex >= count($apps) ? 0 : $nextIndex;
if ($nextIndex) {
$this->assign("next_app", $apps[$nextIndex]);
}
$this->assign("app", $app);
$this->assign("new_menus", $newMenus);
return $this->fetch();
}
}
```
- 附录
- 逻辑与数据模型
- HTML约定-bootstrap
- HTML约定-LayUI
- 角色管理 role
- 数据表
- 列表
- 控制器
- 视图
- 视图-layui
- 角色添加
- 控制器
- 视图
- 角色添加提交
- 控制器
- 验证
- 角色修改
- 控制器
- 视图
- 角色修改提交
- 控制器
- 角色删除
- 控制器
- 权限设置
- 控制器
- 视图
- 模型
- 权限设置提交
- 用户 user
- 数据表
- 列表
- 控制器
- 视图
- 模型
- 添加
- 控制器
- 视图
- 添加提交
- 控制器
- 验证器
- 编辑
- 控制器
- 视图
- 编辑提交
- 控制器
- 验证器
- 删除
- 停用启用
- 角色用户中间表role_user
- 菜单管理
- 数据表
- 表格折叠列表
- 控制器
- 视图
- 全部列表
- 控制器
- 视图
- 填加
- 控制器
- 视图
- 填加提交
- 控制器
- 验证器
- 编辑
- 控制器
- 视图
- 编辑提交
- 控制器
- 验证器
- 删除
- 控制器
- 视图
- 导入菜单
- 视图
- 控制器
- 特别注意
- annotation
- 导出菜单
- 视图
- 控制器
- 排序
- 控制器
- 视图
- 模型