<?php
namespace App\Controller;
use App\Entity\Hero;
use App\Entity\Comment;
use App\Form\HeroType;
use App\Form\CommentType;
use App\Repository\HeroRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\String\Slugger\SluggerInterface;
use App\Service\FileUploader;
#[Route('/hero')]
class HeroController extends AbstractController
{
#[Route('/', name: 'app_hero_index', methods: ['GET'])]
public function index(HeroRepository $heroRepository): Response
{
return $this->render('hero/index.html.twig', [
'heroes' => $heroRepository->findAll(),
]);
}
#[Route('/new', name: 'app_hero_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager, SluggerInterface $slugger, FileUploader $fileUploader): Response
{
$hero = new Hero();
$form = $this->createForm(HeroType::class, $hero);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var UploadedFile $heroFile */
$heroFile = $form->get('hero_filename')->getData();
if ($heroFile) {
$heroFileName = $fileUploader->upload($heroFile);
$hero->setHeroFilename($heroFileName);
}
// ... persist the $hero variable or any other work
$entityManager->persist($hero);
$entityManager->flush();
return $this->redirectToRoute('app_hero_index');
}
return $this->renderForm('hero/new.html.twig', [
'hero' => $hero,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_hero_show', methods: ['GET'])]
public function show(Hero $hero): Response
{
return $this->render('hero/show.html.twig', [
'hero' => $hero,
]);
}
#[Route('/protected/{id}', name: 'app_hero_protected_show', methods: ['GET'])]
public function showProtected(Request $request, Hero $hero, HeroRepository $heroRepository): Response
{
$allComments=$heroRepository->findCommentsByHeroId($hero->getId());
//start
$comment = new Comment();
$comment->addCommentToHero($hero);
$form = $this->createForm(CommentType::class, $comment);
return $this->renderForm('comment/show_protected.html.twig', [
'comment' => $comment,
'form' => $form,
'allComments' => $allComments,
'hero' => $hero
]);
//end
}
#[Route('/{id}/edit', name: 'app_hero_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Hero $hero, EntityManagerInterface $entityManager, FileUploader $fileUploader): Response
{
$form = $this->createForm(HeroType::class, $hero);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var UploadedFile $heroFile */
$heroFile = $form->get('hero_filename')->getData();
$oldHeroFileName = $hero->getHeroFileName();
if ($heroFile) {
$heroFileName = $fileUploader->upload($heroFile);
if(is_null($oldHeroFileName) || $oldHeroFileName !== $heroFileName){
$hero->setHeroFilename($heroFileName);
}
}
// ... persist the $hero variable or any other work
$entityManager->persist($hero);
$entityManager->flush();
return $this->redirectToRoute('app_hero_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('hero/edit.html.twig', [
'hero' => $hero,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_hero_delete', methods: ['POST'])]
public function delete(Request $request, Hero $hero, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$hero->getId(), $request->request->get('_token'))) {
$entityManager->remove($hero);
$entityManager->flush();
}
return $this->redirectToRoute('app_hero_index', [], Response::HTTP_SEE_OTHER);
}
}