src/Controller/UserController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\UserType;
  5. use App\Repository\UserRepository;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use App\Repository\DemandeRepository;
  12. /**
  13.  * @Route("/user")
  14.  */
  15. class UserController extends AbstractController
  16. {
  17.     /**
  18.      * @Route("/index", name="app_user_index", methods={"GET"})
  19.      * 
  20.      */
  21.     public function index(UserRepository $userRepository): Response
  22.     {
  23.         return $this->render('user/index.html.twig', [
  24.             'users' => $userRepository->findAll(),
  25.         ]);
  26.     }
  27.     /**
  28.      * @Route("/new", name="app_user_new", methods={"GET", "POST"})
  29.      */
  30.     public function new(Request $requestUserRepository $userRepositoryUserPasswordEncoderInterface $encoder): Response
  31.     {
  32.         $user = new User();
  33.         $form $this->createForm(UserType::class, $user);
  34.         $form->handleRequest($request);
  35.         if ($form->isSubmitted() && $form->isValid()) {
  36.             $hash $encoder->encodePassword($user$user->getPassword());
  37.             $user->setPassword($hash);
  38.             $userRepository->add($usertrue);
  39.             return $this->redirectToRoute('app_livre_index', [], Response::HTTP_SEE_OTHER);
  40.         }
  41.         return $this->renderForm('user/new.html.twig', [
  42.             'user' => $user,
  43.             'form' => $form,
  44.         ]);
  45.     }
  46.     /**
  47.      * @Route("/profile", name="profile", methods={"GET"})
  48.      */
  49.     public function profile(): Response
  50.     {
  51.         return $this->render('user/profile.html.twig', [
  52.         ]);
  53.     }
  54.     /**
  55.      * @Route("/{id}", name="app_user_show", methods={"GET"})
  56.      */
  57.     public function show(User $user): Response
  58.     {
  59.         $livres $user->getLivres();
  60.         return $this->render('user/show.html.twig', [
  61.             'user' => $user,
  62.             'livres' => $livres,
  63.         ]);
  64.     }
  65.     /**
  66.      * @Route("/echange/{id}/{demande}", name="app_user_echange", methods={"GET"})
  67.      */
  68.     public function echange(User $user$demandeDemandeRepository $demandeRepo): Response
  69.     {
  70.         $livres $user->getLivres();
  71.         $demandeobject $demandeRepo->findBy(['id' => $demande]);
  72.         // dd($demandeobject);
  73.         return $this->render('user/echange.html.twig', [
  74.             'user' => $user,
  75.             'livres' => $livres,
  76.             'demande' => $demande,
  77.             'demandeob' => $demandeobject[0],
  78.         ]);
  79.     }
  80.     
  81.     /**
  82.      * @Route("/{id}/edit", name="app_user_edit", methods={"GET", "POST"})
  83.      */
  84.     public function edit(Request $requestUser $userUserRepository $userRepository): Response
  85.     {
  86.         $form $this->createForm(UserType::class, $user);
  87.         $form->handleRequest($request);
  88.         if ($form->isSubmitted() && $form->isValid()) {
  89.             $userRepository->add($usertrue);
  90.             return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER);
  91.         }
  92.         return $this->renderForm('user/edit.html.twig', [
  93.             'user' => $user,
  94.             'form' => $form,
  95.         ]);
  96.     }
  97.     /**
  98.      * @Route("/{id}", name="app_user_delete", methods={"POST"})
  99.      */
  100.     public function delete(Request $requestUser $userUserRepository $userRepository): Response
  101.     {
  102.         if ($this->isCsrfTokenValid('delete'.$user->getId(), $request->request->get('_token'))) {
  103.             $userRepository->remove($usertrue);
  104.         }
  105.         return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER);
  106.     }
  107. }