|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Controller; |
| 4 | + |
| 5 | +use App\Entity\SweetFood; |
| 6 | +use App\Form\SweetFoodType; |
| 7 | +use App\Repository\SweetFoodRepository; |
| 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 | + |
| 14 | +#[Route('/sweet/food')] |
| 15 | +class SweetFoodController extends AbstractController |
| 16 | +{ |
| 17 | + #[Route('/', name: 'app_sweet_food_index', methods: ['GET'])] |
| 18 | + public function index(SweetFoodRepository $sweetFoodRepository): Response |
| 19 | + { |
| 20 | + return $this->renderForm('sweet_food/index.html.twig', [ |
| 21 | + 'sweet_foods' => $sweetFoodRepository->findAll(), |
| 22 | + ]); |
| 23 | + } |
| 24 | + |
| 25 | + #[Route('/new', name: 'app_sweet_food_new', methods: ['GET', 'POST'])] |
| 26 | + public function new(Request $request, EntityManagerInterface $entityManager): Response |
| 27 | + { |
| 28 | + $sweetFood = new SweetFood(); |
| 29 | + $form = $this->createForm(SweetFoodType::class, $sweetFood); |
| 30 | + $form->handleRequest($request); |
| 31 | + |
| 32 | + if ($form->isSubmitted() && $form->isValid()) { |
| 33 | + $entityManager->persist($sweetFood); |
| 34 | + $entityManager->flush(); |
| 35 | + |
| 36 | + return $this->redirectToRoute('app_sweet_food_index', [], Response::HTTP_SEE_OTHER); |
| 37 | + } |
| 38 | + |
| 39 | + return $this->renderForm('sweet_food/new.html.twig', [ |
| 40 | + 'sweet_food' => $sweetFood, |
| 41 | + 'form' => $form, |
| 42 | + ]); |
| 43 | + } |
| 44 | + |
| 45 | + #[Route('/{id}', name: 'app_sweet_food_show', methods: ['GET'])] |
| 46 | + public function show(SweetFood $sweetFood): Response |
| 47 | + { |
| 48 | + return $this->renderForm('sweet_food/show.html.twig', [ |
| 49 | + 'sweet_food' => $sweetFood, |
| 50 | + ]); |
| 51 | + } |
| 52 | + |
| 53 | + #[Route('/{id}/edit', name: 'app_sweet_food_edit', methods: ['GET', 'POST'])] |
| 54 | + public function edit(Request $request, SweetFood $sweetFood, EntityManagerInterface $entityManager): Response |
| 55 | + { |
| 56 | + $form = $this->createForm(SweetFoodType::class, $sweetFood); |
| 57 | + $form->handleRequest($request); |
| 58 | + |
| 59 | + if ($form->isSubmitted() && $form->isValid()) { |
| 60 | + $entityManager->flush(); |
| 61 | + |
| 62 | + return $this->redirectToRoute('app_sweet_food_index', [], Response::HTTP_SEE_OTHER); |
| 63 | + } |
| 64 | + |
| 65 | + return $this->renderForm('sweet_food/edit.html.twig', [ |
| 66 | + 'sweet_food' => $sweetFood, |
| 67 | + 'form' => $form, |
| 68 | + ]); |
| 69 | + } |
| 70 | + |
| 71 | + #[Route('/{id}', name: 'app_sweet_food_delete', methods: ['POST'])] |
| 72 | + public function delete(Request $request, SweetFood $sweetFood, EntityManagerInterface $entityManager): Response |
| 73 | + { |
| 74 | + if ($this->isCsrfTokenValid('delete'.$sweetFood->getId(), $request->request->get('_token'))) { |
| 75 | + $entityManager->remove($sweetFood); |
| 76 | + $entityManager->flush(); |
| 77 | + } |
| 78 | + |
| 79 | + return $this->redirectToRoute('app_sweet_food_index', [], Response::HTTP_SEE_OTHER); |
| 80 | + } |
| 81 | +} |
0 commit comments