src/Controller/ProjetsController.php line 62

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Projets;
  4. use App\Form\ProjetsType;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\String\Slugger\SluggerInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  12. class ProjetsController extends AbstractController
  13. {
  14.     public function __construct(EntityManagerInterface $manager){
  15.         $this->manager $manager;
  16.     }
  17.     /**
  18.      * @Route("/admin/projets", name="app_projets")
  19.      */
  20.     public function index(Request $requestSluggerInterface $slugger): Response
  21.     {
  22.         $projets = new Projets();
  23.         $formProjets $this->createForm(ProjetsType::class, $projets);
  24.         $formProjets->handleRequest($request);
  25.         if ($formProjets->isSubmitted() && $formProjets->isValid()) {
  26.             $photoProjets $formProjets->get('image')->getData();
  27.             if($photoProjets){
  28.                 $originalFilename pathinfo
  29.                 ($photoProjets->getClientOriginalName(),PATHINFO_FILENAME);
  30.                 $safeFilename $slugger->slug($originalFilename);
  31.                 $newFilename $safeFilename.'-'.uniqid().'.'.
  32.                 $photoProjets->guessExtension();
  33.                 try{
  34.                     $photoProjets->move(
  35.                         $this->getParameter('image'),
  36.                         $newFilename
  37.                     );
  38.                 }catch (FileException $e){
  39.                 }
  40.                 $projets->setImage($newFilename);
  41.             }
  42.             
  43.             $this->manager->persist($projets);
  44.             $this->manager->flush();
  45.             return $this->redirectToRoute('app_projets');
  46.         };
  47.         return $this->render('projets/index.html.twig', [
  48.             'formProjets' => $formProjets->createView(),
  49.         ]);
  50.     }
  51.     /**
  52.      * @Route("/all/projets", name="app_all_projets")
  53.      */
  54.     public function affichage(): Response
  55.     {
  56.         $allProjets $this->manager->getRepository(Projets::class)->findAll();
  57.         return $this->render('projets/allProjets.html.twig', [
  58.             'Projets' => $allProjets,
  59.         ]);
  60.     }
  61.     
  62.      /**
  63.      * @Route("/admin/delete/projets{id}", name="app_admin_delete_projets")
  64.      */
  65.     public function delete(Projets $projets): Response
  66.     {
  67.        $this->manager->remove($projets);
  68.        $this->manager->flush();
  69.        return $this->redirectToRoute('app_all_projets');
  70.     }
  71.      /**
  72.      * @Route("/single/projets{id}", name="app_single_projets")
  73.      */
  74.     public function affichageprojet(Projets $projets): Response
  75.     {
  76.         $Projets $this->manager->getRepository(Projets::class)->find($projets);
  77.         return $this->render('projets/singleprojets.html.twig', [
  78.             'Projets' => $Projets,
  79.         ]);
  80.     }
  81.      /**
  82.      * @Route("/admin/edit/projets/{id}", name="app_admin_edit_projets")
  83.      */
  84.     public function edit(Projets $projetsRequest $requestSluggerInterface $slugger): Response
  85.     {
  86.         $form $this->createForm(ProjetsType::class, $projets); // Création du formulaire
  87.         $form->handleRequest($request); // Traitement du formulaire
  88.         if ($form->isSubmitted() && $form->isValid()) {
  89.             $photoProjets $form->get('image')->getData();
  90.        
  91.             if($photoProjets){
  92.            $originalFilename pathinfo($photoProjets->getClientOriginalName(),PATHINFO_FILENAME);
  93.            $safeFilename $slugger->slug($originalFilename);
  94.            $newFilename $safeFilename.'-'.uniqid().'.'.$photoProjets->guessExtension();
  95.              try {
  96.                 $photoProjets->move(
  97.                     $this->getParameter('image'),
  98.                     $newFilename
  99.                 );
  100.              }catch (FileException $e){
  101.              }
  102.               $projets->setImage($newFilename);
  103.             
  104.             }
  105.             $this->manager->persist($projets);
  106.             $this->manager->flush();
  107.             return $this->redirectToRoute('app_home');
  108.     };
  109.     return $this->render('projets/edit.html.twig', [
  110.         'formProjets' => $form->createView(),
  111.     ]);
  112.     }
  113. }