src/Controller/HeroController.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Hero;
  4. use App\Entity\Comment;
  5. use App\Form\HeroType;
  6. use App\Form\CommentType;
  7. use App\Repository\HeroRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  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. use Symfony\Component\String\Slugger\SluggerInterface;
  14. use App\Service\FileUploader;
  15. #[Route('/hero')]
  16. class HeroController extends AbstractController
  17. {
  18.     #[Route('/'name'app_hero_index'methods: ['GET'])]
  19.     public function index(HeroRepository $heroRepository): Response
  20.     {
  21.         return $this->render('hero/index.html.twig', [
  22.             'heroes' => $heroRepository->findAll(),
  23.         ]);
  24.     }
  25.     #[Route('/new'name'app_hero_new'methods: ['GET''POST'])]
  26.     public function new(Request $requestEntityManagerInterface $entityManagerSluggerInterface $sluggerFileUploader $fileUploader): Response
  27.     {
  28.         $hero = new Hero();
  29.         $form $this->createForm(HeroType::class, $hero);
  30.         $form->handleRequest($request);
  31.         if ($form->isSubmitted() && $form->isValid()) {
  32.             /** @var UploadedFile $heroFile */
  33.             $heroFile $form->get('hero_filename')->getData();
  34.             if ($heroFile) {
  35.                 $heroFileName $fileUploader->upload($heroFile);
  36.                 $hero->setHeroFilename($heroFileName);
  37.             }
  38.             // ... persist the $hero variable or any other work
  39.             $entityManager->persist($hero);
  40.             $entityManager->flush();
  41.             return $this->redirectToRoute('app_hero_index');
  42.         }
  43.         return $this->renderForm('hero/new.html.twig', [
  44.             'hero' => $hero,
  45.             'form' => $form,
  46.         ]);
  47.     }
  48.     #[Route('/{id}'name'app_hero_show'methods: ['GET'])]
  49.     public function show(Hero $hero): Response
  50.     {
  51.         return $this->render('hero/show.html.twig', [
  52.             'hero' => $hero,
  53.         ]);
  54.     }
  55.     #[Route('/protected/{id}'name'app_hero_protected_show'methods: ['GET'])]
  56.     public function showProtected(Request $requestHero $heroHeroRepository $heroRepository): Response
  57.     {
  58.         $allComments=$heroRepository->findCommentsByHeroId($hero->getId());
  59.         //start
  60.         $comment = new Comment();
  61.         $comment->addCommentToHero($hero);
  62.         $form $this->createForm(CommentType::class, $comment);
  63.         return $this->renderForm('comment/show_protected.html.twig', [
  64.             'comment' => $comment,
  65.             'form' => $form,
  66.             'allComments' => $allComments,
  67.             'hero' => $hero
  68.         ]);
  69.         //end
  70.     }   
  71.     #[Route('/{id}/edit'name'app_hero_edit'methods: ['GET''POST'])]
  72.     public function edit(Request $requestHero $heroEntityManagerInterface $entityManagerFileUploader $fileUploader): Response
  73.     {
  74.         $form $this->createForm(HeroType::class, $hero);
  75.         $form->handleRequest($request);
  76.         if ($form->isSubmitted() && $form->isValid()) {
  77.             /** @var UploadedFile $heroFile */
  78.             $heroFile $form->get('hero_filename')->getData();
  79.             $oldHeroFileName $hero->getHeroFileName();
  80.             if ($heroFile) {
  81.                 $heroFileName $fileUploader->upload($heroFile);
  82.                 if(is_null($oldHeroFileName) || $oldHeroFileName !== $heroFileName){
  83.                     $hero->setHeroFilename($heroFileName);
  84.                 }
  85.             }
  86.             // ... persist the $hero variable or any other work
  87.             $entityManager->persist($hero);
  88.             $entityManager->flush();
  89.             return $this->redirectToRoute('app_hero_index', [], Response::HTTP_SEE_OTHER);
  90.         }
  91.         return $this->renderForm('hero/edit.html.twig', [
  92.             'hero' => $hero,
  93.             'form' => $form,
  94.         ]);
  95.     }
  96.     #[Route('/{id}'name'app_hero_delete'methods: ['POST'])]
  97.     public function delete(Request $requestHero $heroEntityManagerInterface $entityManager): Response
  98.     {
  99.         if ($this->isCsrfTokenValid('delete'.$hero->getId(), $request->request->get('_token'))) {
  100.             $entityManager->remove($hero);
  101.             $entityManager->flush();
  102.         }
  103.         return $this->redirectToRoute('app_hero_index', [], Response::HTTP_SEE_OTHER);
  104.     }
  105. }