push de controller cuisinier /add /delete

This commit is contained in:
bayard 2024-10-17 17:53:18 +02:00
parent df467e2b77
commit 9dfd212718

View File

@ -0,0 +1,46 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class CuisinierController extends AbstractController
{
#[Route('/cuisinier/ajouter', name: 'ajouter_cuisinier')]
public function ajouter(Request $request, EntityManagerInterface $entityManager): Response
{
$cuisinier = new Cuisinier();
$form = $this->createForm(CuisinierType::class, $cuisinier);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($cuisinier);
$entityManager->flush();
$this->addFlash('success', 'Cuisinier ajouté avec succès !');
return $this->redirectToRoute('ajouter_cuisinier');
}
return $this->render('cuisinier/ajouter.html.twig', [
'form' => $form->createView(),
]);
}
#[Route('/cuisinier/supprimer/{id}', name: 'supprimer_cuisinier', methods: ['POST'])]
public function supprimer(int $id, EntityManagerInterface $entityManager): Response
{
$cuisinier = $entityManager->getRepository(Cuisinier::class)->find($id);
if (!$cuisinier) {
throw $this->createNotFoundException('Ce cuisinier n\'existe pas');
}
$entityManager->remove($cuisinier);
$entityManager->flush();
$this->addFlash('success', 'Cuisinier supprimé avec succès !');
return $this->redirectToRoute('ajouter_cuisinier');
}
}