src/Controller/CategoryController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Category;
  4. use App\Form\CategoryType;
  5. use App\Repository\CategoryRepository;
  6. use App\Repository\PropertiesRepository;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. /**
  12.  * @Route("/category")
  13.  */
  14. class CategoryController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/", name="app_category_admin", methods={"GET"})
  18.      */
  19.     public function showAdmin(CategoryRepository $categoryRepository): Response
  20.     {
  21.         return $this->render('category/admin.html.twig', [
  22.             'categories' => $categoryRepository->findAll(),
  23.         ]);
  24.     }
  25.     /**
  26.      * @Route("/", name="app_category_index", methods={"GET"})
  27.      */
  28.     public function index(CategoryRepository $categoryRepository): Response
  29.     {
  30.         return $this->render('category/index.html.twig', [
  31.             'categories' => $categoryRepository->findAll(),
  32.         ]);
  33.     }
  34.     /**
  35.      * @Route("/new", name="app_category_new", methods={"GET", "POST"})
  36.      */
  37.     public function new(Request $requestCategoryRepository $categoryRepository): Response
  38.     {
  39.         $category = new Category();
  40.         $form $this->createForm(CategoryType::class, $category);
  41.         $form->handleRequest($request);
  42.         if ($form->isSubmitted() && $form->isValid()) {
  43.             $categoryRepository->add($categorytrue);
  44.             return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER);
  45.         }
  46.         return $this->renderForm('category/new.html.twig', [
  47.             'category' => $category,
  48.             'form' => $form,
  49.         ]);
  50.     }
  51.     /**
  52.      * @Route("/{name}", name="app_category_show", methods={"GET"})
  53.      */
  54.     public function show(PropertiesRepository $propertyRepositoryCategory $category): Response
  55.     {  
  56.         $properties $propertyRepository->findBy(["category" => $category]);
  57.        
  58.         return $this->render('category/show.html.twig', [
  59.             'properties' => $properties,
  60.         ]);
  61.     }
  62.     /**
  63.      * @Route("/{id}/edit", name="app_category_edit", methods={"GET", "POST"})
  64.      */
  65.     public function edit(Request $requestCategory $categoryCategoryRepository $categoryRepository): Response
  66.     {
  67.         $form $this->createForm(CategoryType::class, $category);
  68.         $form->handleRequest($request);
  69.         if ($form->isSubmitted() && $form->isValid()) {
  70.             $categoryRepository->add($categorytrue);
  71.             return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER);
  72.         }
  73.         return $this->renderForm('category/edit.html.twig', [
  74.             'category' => $category,
  75.             'form' => $form,
  76.         ]);
  77.     }
  78.     /**
  79.      * @Route("/{id}", name="app_category_delete", methods={"POST"})
  80.      */
  81.     public function delete(Request $requestCategory $categoryCategoryRepository $categoryRepository): Response
  82.     {
  83.         if ($this->isCsrfTokenValid('delete' $category->getId(), $request->request->get('_token'))) {
  84.             $categoryRepository->remove($categorytrue);
  85.         }
  86.         return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER);
  87.     }
  88. }