src/Controller/PostController.php line 126

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Post;
  4. use App\Form\PostType;
  5. use App\Entity\User;
  6. use App\Repository\PostRepository;
  7. use Doctrine\ORM\Mapping\Id;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. /**
  14.  * @Route("/post")
  15.  */
  16. class PostController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/", name="post_index", methods={"GET"})
  20.      * @IsGranted("ROLE_USER", message="No access! Get out!")
  21.      */
  22.     public function index(PostRepository $postRepository): Response
  23.     {
  24.         $post $postRepository->findAll();
  25.         //dump($post);die();
  26.         return $this->render('post/index.html.twig', [
  27.             'posts' => $post,
  28.         ]);
  29.     }
  30.     /**
  31.      * @Route("/new", name="post_new", methods={"GET","POST"})
  32.      */
  33.     public function new(Request $request): Response
  34.     {
  35.         $post = new Post();
  36.         $form $this->createForm(PostType::class, $post);
  37.         $form->handleRequest($request);
  38.         if ($form->isSubmitted() && $form->isValid()) {
  39.             $entityManager $this->getDoctrine()->getManager();
  40.            // $image=$form->getData()->getImageFile();
  41.            // dump($form,$image);die();
  42.             $post->setCreatAt(new \DateTime());
  43.             $post->setUpdateAt(new \DateTime());
  44.             $post->setDeleteAt(new  \DateTime());
  45.             $post->setSlug($post->getTitle().'-'.uniqid());
  46.             $nom=$this->getUser();
  47.             $post->setAuteur($nom);
  48.             $entityManager->persist($post);
  49.             $entityManager->flush();
  50.            // $entityManager->flush();
  51.             $this->addFlash(
  52.                 'success',
  53.                 "Votre enregistrement  a été effectué  avec succès!"
  54.             );
  55.             return $this->redirectToRoute('post_index');
  56.         }
  57.         return $this->render('post/new.html.twig', [
  58.             'post' => $post,
  59.             'form' => $form->createView(),
  60.         ]);
  61.     }
  62.     /**
  63.      * @Route("/{id}", name="post_show", methods={"GET"})
  64.      */
  65.     public function show(Post $post): Response
  66.     {
  67.         return $this->render('post/show.html.twig', [
  68.             'post' => $post,
  69.         ]);
  70.     }
  71.     /**
  72.      * @Route("/{id}/edit", name="post_edit", methods={"GET","POST"})
  73.      */
  74.     public function edit(Request $requestPost $post): Response
  75.     {
  76.         $form $this->createForm(PostType::class, $post);
  77.         $form->handleRequest($request);
  78.         if ($form->isSubmitted() && $form->isValid()) {
  79.             $post->setSlug($post->getTitle().'-'.uniqid());
  80.             $this->getDoctrine()->getManager()->flush();
  81.             $post->setAuteur($this->getUser());
  82.             $this->addFlash(
  83.                 'success',
  84.                 "Votre modification  a été effectué  avec succès!"
  85.             );
  86.             return $this->redirectToRoute('post_index');
  87.         }
  88.         return $this->render('post/edit.html.twig', [
  89.             'post' => $post,
  90.             'form' => $form->createView(),
  91.         ]);
  92.     }
  93.     /**
  94.      * @Route("/{id}", name="post_delete", methods={"DELETE"})
  95.      */
  96.     public function delete(Request $requestPost $post): Response
  97.     {
  98.         if ($this->isCsrfTokenValid('delete'.$post->getId(), $request->request->get('_token'))) {
  99.             $entityManager $this->getDoctrine()->getManager();
  100.             $entityManager->remove($post);
  101.             $entityManager->flush();
  102.         }
  103.         return $this->redirectToRoute('post_index');
  104.     }
  105.     /**
  106.      * @Route("/article/{id}", name="post_show", methods={"GET"})
  107.      */
  108.     public function postshow(String $id): Response
  109.     {
  110.         $repository $this->getDoctrine()->getRepository(Post::class);
  111.         $post $repository->findOneBy([
  112.             'id'=>$id
  113.         ]);
  114.         $latest$repository->findBylast();
  115.         return $this->render('front/article.html.twig', [
  116.             'post' => $post,
  117.             'latest' => $latest,
  118.         ]);
  119.     }
  120.     /**
  121.      * @Route("/information/{id}", name="new_post_show", methods={"GET"})
  122.      */
  123.     public function newpostshow(String $id): Response
  124.     {
  125.         $repository $this->getDoctrine()->getRepository(Post::class);
  126.         $post $repository->findOneBy([
  127.             'id'=>$id
  128.         ]);
  129.         //dd($post);
  130.         $latest$repository->findBylast();
  131.         //dd($latest);
  132.         return $this->render('newwebfront/article.html.twig', [
  133.             'post' => $post,
  134.             'latest' => $latest,
  135.         ]);
  136.     }
  137. }