hegresphere/src/Controller/FAQController.php
2024-12-12 17:04:32 +01:00

84 lines
2.7 KiB
PHP

<?php
namespace App\Controller;
use App\Entity\FAQ;
use App\Form\FAQType;
use App\Repository\FAQRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/faq')]
final class FAQController extends AbstractController
{
#[Route(name: 'app_faq_index', methods: ['GET'])]
public function index(FAQRepository $fAQRepository): Response
{
return $this->render('faq/index.html.twig', [
'faqs' => $fAQRepository->findAll(),
]);
}
#[Route('/new', name: 'app_faq_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
$fAQ = new FAQ();
$form = $this->createForm(FAQType::class, $fAQ);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$fAQ->setUpdateDate(new \DateTime());
$entityManager->persist($fAQ);
$entityManager->flush();
return $this->redirectToRoute('app_faq_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('faq/new.html.twig', [
'faq' => $fAQ,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_faq_show', methods: ['GET'])]
public function show(FAQ $fAQ): Response
{
return $this->render('faq/show.html.twig', [
'faq' => $fAQ,
]);
}
#[Route('/{id}/edit', name: 'app_faq_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, FAQ $fAQ, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(FAQType::class, $fAQ);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$fAQ->setUpdateDate(new \DateTime());
$entityManager->flush();
return $this->redirectToRoute('app_faq_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('faq/edit.html.twig', [
'faq' => $fAQ,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_faq_delete', methods: ['POST'])]
public function delete(Request $request, FAQ $fAQ, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$fAQ->getId(), $request->getPayload()->getString('_token'))) {
$entityManager->remove($fAQ);
$entityManager->flush();
}
return $this->redirectToRoute('app_faq_index', [], Response::HTTP_SEE_OTHER);
}
}