CategoryController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Http\Controllers\Backend;
  3. use Illuminate\Http\Request;
  4. use App\Http\Requests;
  5. use App\Http\Controllers\Controller;
  6. use App\Repositories\CategoryRepositoryEloquent;
  7. use App\Http\Requests\Backend\Category\CreateRequest;
  8. use App\Http\Requests\Backend\Category\UpdateRequest;
  9. use App\Repositories\NavigationRepositoryEloquent;
  10. use Mockery\CountValidator\Exception;
  11. class CategoryController extends Controller
  12. {
  13. protected $category;
  14. public function __construct(CategoryRepositoryEloquent $category)
  15. {
  16. $this->category = $category;
  17. }
  18. /**
  19. * Display a listing of the resource.
  20. *
  21. * @return \Illuminate\Http\Response
  22. */
  23. public function index()
  24. {
  25. $category = $this->category->getNestedList();
  26. return view("backend.category.index", compact('category'));
  27. }
  28. /**
  29. * Show the form for creating a new resource.
  30. *
  31. * @return \Illuminate\Http\Response
  32. */
  33. public function create()
  34. {
  35. return view('backend.category.create');
  36. }
  37. /**
  38. * Store a newly created resource in storage.
  39. *
  40. * @param \Illuminate\Http\Request $request
  41. * @return \Illuminate\Http\Response
  42. */
  43. public function store(CreateRequest $request)
  44. {
  45. $result = $this->category->store($request->all());
  46. if ($result) {
  47. return redirect('backend/category')->with('success', '分类添加成功');
  48. }
  49. return redirect(route('backend.category.create'))->withErrors('系统异常,分类添加失败');
  50. }
  51. /**
  52. * Display the specified resource.
  53. *
  54. * @param int $id
  55. * @return \Illuminate\Http\Response
  56. */
  57. public function show($id)
  58. {
  59. //
  60. }
  61. /**
  62. * Show the form for editing the specified resource.
  63. *
  64. * @param int $id
  65. * @return \Illuminate\Http\Response
  66. */
  67. public function edit($id)
  68. {
  69. $category = $this->category->find($id);
  70. return view('backend.category.edit', compact('category'));
  71. }
  72. /**
  73. * Update the specified resource in storage.
  74. *
  75. * @param \Illuminate\Http\Request $request
  76. * @param int $id
  77. * @return \Illuminate\Http\Response
  78. */
  79. public function update(UpdateRequest $request, $id)
  80. {
  81. $result = $this->category->update($request->all(), $id);
  82. if ($result) {
  83. return redirect('backend/category')->with('success', '分类修改成功');
  84. }
  85. return redirect(route('backend.category.edit', ['id' => $id]))->withErrors('系统异常,分类修改失败');
  86. }
  87. /**
  88. * Remove the specified resource from storage.
  89. *
  90. * @param int $id
  91. * @return \Illuminate\Http\Response
  92. */
  93. public function destroy($id)
  94. {
  95. if ($this->category->delete($id)) {
  96. return response()->json(['status' => 0]);
  97. }
  98. return response()->json(['status' => 1]);
  99. }
  100. /**
  101. * @param $id
  102. * @param NavigationRepositoryEloquent $nav
  103. * @return \Illuminate\Http\JsonResponse
  104. */
  105. public function setNavigation($id, NavigationRepositoryEloquent $nav)
  106. {
  107. try {
  108. $category = $this->category->find($id);
  109. if ($nav->setCategoryNav($category->id, $category->name)) {
  110. return redirect()->back()->with('success', '设置成功');
  111. }
  112. throw new Exception('设置失败');
  113. } catch (\Exception $e) {
  114. dd($e->getMessage());
  115. return redirect()->back()->withErrors($e->getMessage());
  116. }
  117. }
  118. }