src/Controller/Back/SubCategoryController.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Back;
  3. use App\Entity\Category;
  4. use App\Entity\SubCategory;
  5. use App\Form\CategoryType;
  6. use App\Form\SubCategoryType;
  7. use App\Repository\SubCategoryRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\String\Slugger\SluggerInterface;
  13. /**
  14.  * @Route("/admin")
  15.  */
  16. class SubCategoryController extends AbstractController
  17. {
  18.     /**
  19.      * @var EntityManagerInterface
  20.      */
  21.     private EntityManagerInterface $em;
  22.     /**
  23.      * @var SluggerInterface
  24.      */
  25.     private SluggerInterface $slugger;
  26.     public function __construct(EntityManagerInterface $emSluggerInterface $slugger)
  27.     {
  28.         $this->em $em;
  29.         $this->slugger $slugger;
  30.     }
  31.     /**
  32.      * @Route("/sub-category/list", name="back_subcategory_list")
  33.      */
  34.     public function list(SubCategoryRepository $subCategoryRepository)
  35.     {
  36.         return $this->render('back/subcategory/list.html.twig', [
  37.             'subcategories' => $subCategoryRepository->findBy([], ['id' => 'DESC'])
  38.         ]);
  39.     }
  40.     /**
  41.      * @Route("/sub-category/add", name="back_subcategory_add")
  42.      */
  43.     public function add(Request $request)
  44.     {
  45.         $subCategory = new SubCategory();
  46.         $form $this->createForm(SubCategoryType::class, $subCategory)->handleRequest($request);
  47.         if ($form->isSubmitted() && $form->isValid()) {
  48.             $sluggedName $this->slugger->slug($subCategory->getName())->lower();
  49.             $subCategory->setSlug($sluggedName);
  50.             $this->em->persist($subCategory);
  51.             $this->em->flush();
  52.             $this->addFlash('success''Sous-catégorie créée');
  53.             return $this->redirectToRoute('back_subcategory_list');
  54.         }
  55.         return $this->render('back/subcategory/add.html.twig', [
  56.             'form' => $form->createView()
  57.         ]);
  58.     }
  59.     /**
  60.      * @Route("/sub-category/edit/{id}", name="back_subcategory_edit")
  61.      */
  62.     public function edit(SubCategory $subCategoryRequest $request)
  63.     {
  64.         $form $this->createForm(SubCategoryType::class, $subCategory)->handleRequest($request);
  65.         if ($form->isSubmitted() && $form->isValid()) {
  66.             $sluggedName $this->slugger->slug($subCategory->getName())->lower();
  67.             $subCategory->setSlug($sluggedName);
  68.             $this->em->flush();
  69.             $this->addFlash('success''Sous-catégorie éditée');
  70.             return $this->redirectToRoute('back_subcategory_list');
  71.         }
  72.         return $this->render('back/subcategory/edit.html.twig', [
  73.             'form' => $form->createView()
  74.         ]);
  75.     }
  76.     /**
  77.      * @Route("/sub-category/remove/{id}", name="back_subcategory_remove")
  78.      */
  79.     public function remove(SubCategory $subCategory)
  80.     {
  81.         $this->em->remove($subCategory);
  82.         $this->em->flush();
  83.         $this->addFlash('success''Sous-catégorie supprimée');
  84.         return $this->redirectToRoute('back_subcategory_list');
  85.     }
  86. }