在设计权限库时遇到了对象和数组生成的问题,欢迎大家来讨论
思路: 通过数据表(menu)来记录菜单栏与子菜单, 然后通过权限表给对应用户分配权限;
数据表:
| id | fatherId | title |
| ———— | ———— | ———— |
| 1 | 0 | 管理员管理 |
| 2 | 0 | 用户管理 |
| 3 | 2 | 账号管理 |
| 4 | 2 | 权限管理 |
| 5 | 0 | 部门管理 |
| 6 | 0 | 部门组管理 |
理想生成数据格式:
[{“title”:”控制台”},{“title”:”管理员管理”,”children”:[{“title”:”账号管理”,”href”:”sys/admin”}]},{“title”:”用户管理”,”children”:[{“title”:”账号管理”,”href”:”sys/user”},{“title”:”权限管理”,”href”:”sys/power”}]},{“title”:”部门管理”},{“title”:”部门组管理”}]
开发思路:
//首先获取所有菜单
$data = DB::table(“menu”)->where(‘isDel’,0)->get();
//遍历数据
$list = [];
foreach ($data as $key => $value) {
//先找到顶级菜单
if ($value->fatherId==0) {
$list[]=$value;
}else{
//找到与顶级相关的子级菜单,并在数组中生成对象
}}
return $list;
在else里面不知道怎么往对象中插入 children,并且往children中加入 子菜单数据…
还原讨论
推荐文章: