em = $em; } # DEBUG: Ne doit pas ĂȘtre laissĂ© en production. #[Route('/', name: 'all post', methods: ['GET'])] public function getAllPost(): Response { $posts = $this->em->getRepository(Post::class)->findAll(); return $this->render('post/all.html.twig', [ "posts" => $posts, "title" => "Derniers Posts" ]); } #[Route( '/post/{id}', name: 'display_post', methods: ['GET'], requirements: ['id' => '\d+'] )] public function getPost(int $id): Response { $post = $this->em->getRepository(Post::class)->find($id); if (!$post) { } return $this->render('post/post.html.twig', [ 'post' => $post ]); } #[Route('/post/new/', name: 'add_post', methods: ['GET', 'POST'])] public function addPost(Request $request): Response { $this->denyAccessUnlessGranted('IS_AUTHENTICATED'); $post = new Post(); $form = $this->createForm(PostType::class, $post); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { //$dateNow = new DateTime(); $form = $form->getData(); $user = $this->getUser(); $post->setProfil($user); $this->em->persist($post); $this->em->flush(); return $this->redirectToRoute('display_post', ['id' => $post->getId()]); } return $this->render('post/new.html.twig', [ 'form' => $form, ]); } #[Route('/post/{id}', name: 'remove_post', methods: ['DELETE'])] public function removePost(int $id): Response { $post = $this->em->getRepository(Post::class)->find($id); if($post->getProfil()->getId() === $this->getUser()->getId()) { $this->em->remove($post); $this->em->flush(); } return new Response(); } }