src/Controller/CommunityController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use DateTime;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use App\Entity\CommunityPosts;
  9. use App\Form\CommunityPostFormType;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use App\Repository\CommunityPostsRepository;
  12. class CommunityController extends AbstractController
  13. {
  14.     private CommunityPostsRepository $communityPostsRepository;
  15.     public function __construct(CommunityPostsRepository $communityPostsRepository)
  16.     {
  17.         $this->communityPostsRepository $communityPostsRepository;
  18.     }
  19.     #[Route('/community'name'community')]
  20.     public function index(Request $request): RedirectResponse|Response
  21.     {
  22.         $communityComment=new CommunityPosts();
  23.         $form=$this->createForm(CommunityPostFormType::class, $communityComment);
  24.         $form->handleRequest($request);
  25.         if($form->isSubmitted() && $form->isValid()){
  26.             
  27.             $actuallyDate=new DateTime(date("d-m-Y H:i:s"));
  28.             $communityComment=$form->getData();
  29.             $communityComment->setUserId($this->getUser());
  30.             $communityComment->setDatePostComment($actuallyDate);
  31.             $this->communityPostsRepository->add($communityCommenttrue);
  32.             $type="success";
  33.             $message="L'équipe vous remercie d'avoir laissé votre avis.";
  34.             $this->addFlash($type$message);
  35.             return $this->redirectToRoute('community');
  36.         }
  37.         return $this->render('clientWeb/community.html.twig', [
  38.             'formPost' => $form->createView(),
  39.             'listComment'=>$this->communityPostsRepository->findAll(),
  40.         ]);
  41.     }
  42. }