UserController.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Auth;
  4. use App\Http\Requests\UserCreateRequest;
  5. use App\Http\Requests\UserUpdateRequest;
  6. use App\Repositories\UserRepository;
  7. use Illuminate\Http\Request;
  8. class UserController extends Controller
  9. {
  10. protected $userRepository;
  11. protected $nbrPerPage = 4;
  12. public function __construct(UserRepository $userRepository)
  13. {
  14. $this->userRepository = $userRepository;
  15. }
  16. public function index()
  17. {
  18. $users = $this->userRepository->getPaginate($this->nbrPerPage);
  19. $links = $users->setPath('')->render();
  20. return view('user/index', compact('users', 'links'));
  21. }
  22. public function create()
  23. {
  24. return view('user/create');
  25. }
  26. public function store(UserCreateRequest $request)
  27. {
  28. $this->setAdmin($request);
  29. $user = $this->userRepository->store($request->all());
  30. return redirect('user')->withOk("L'utilisateur " . $user->name . " a été créé.");
  31. }
  32. public function show($id)
  33. {
  34. $user = $this->userRepository->getById($id);
  35. return view('user/show', compact('user'));
  36. }
  37. public function edit($id)
  38. {
  39. $user = $this->userRepository->getById($id);
  40. return view('user/edit', compact('user'));
  41. }
  42. public function update(UserUpdateRequest $request, $id)
  43. {
  44. $this->setAdmin($request);
  45. $this->userRepository->update($id, $request->all());
  46. return redirect('user')->withOk("L'utilisateur " . $request->input('name') . " a été modifié.");
  47. }
  48. public function destroy($id)
  49. {
  50. $this->userRepository->destroy($id);
  51. return redirect()->back();
  52. }
  53. private function setAdmin($request)
  54. {
  55. if(!$request->has('admin'))
  56. {
  57. $request->merge(['admin' => 0]);
  58. }
  59. }
  60. }