Laravel框架示例(6.2版本)

为了方便演示,直接用DB类去写 App/Http/Controllers/IndexController.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Support\Facades\DB;
  5. class IndexController extends Controller{
  6. // 获取初始化数据
  7. public function getSystemInit(){
  8. $homeInfo = [
  9. 'title' => '首页',
  10. 'href' => 'page/welcome-1.html?t=1',
  11. ];
  12. $logoInfo = [
  13. 'title' => 'LAYUI MINI',
  14. 'image' => 'images/logo.png',
  15. ];
  16. $menuInfo = $this->getMenuList();
  17. $systemInit = [
  18. 'homeInfo' => $homeInfo,
  19. 'logoInfo' => $logoInfo,
  20. 'menuInfo' => $menuInfo,
  21. ];
  22. return response()->json($systemInit);
  23. }
  24. // 获取菜单列表
  25. private function getMenuList(){
  26. $menuList = DB::table('system_menu')
  27. ->select(['id','pid','title','icon','href','target'])
  28. ->where('status', 1)
  29. ->orderBy('sort', 'desc')
  30. ->get();
  31. $menuList = $this->buildMenuChild(0, $menuList);
  32. return $menuList;
  33. }
  34. //递归获取子菜单
  35. private function buildMenuChild($pid, $menuList){
  36. $treeList = [];
  37. foreach ($menuList as $v) {
  38. if ($pid == $v->pid) {
  39. $node = (array)$v;
  40. $child = $this->buildMenuChild($v->id, $menuList);
  41. if (!empty($child)) {
  42. $node['child'] = $child;
  43. }
  44. // todo 后续此处加上用户的权限判断
  45. $treeList[] = $node;
  46. }
  47. }
  48. return $treeList;
  49. }
  50. }