diff --git a/src/Controller/CategoryController.php b/src/Controller/CategoryController.php new file mode 100644 index 0000000..af71d09 --- /dev/null +++ b/src/Controller/CategoryController.php @@ -0,0 +1,74 @@ +render('category/index.html.twig', [ + 'categories' => $categoryRepository->findAll(), + ]); + } + + #[Route('/new', name: 'app_category_new', methods: ['GET', 'POST'])] + public function new(Request $request, EntityManagerInterface $entityManager): Response + { + $category = new Category(); + $form = $this->createForm(CategoryType::class, $category); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->persist($category); + $entityManager->flush(); + + return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('category/new.html.twig', [ + 'category' => $category, + 'form' => $form, + ]); + } + + + #[Route('/{id}/edit', name: 'app_category_edit', methods: ['GET', 'POST'])] + public function edit(Request $request, Category $category, EntityManagerInterface $entityManager): Response + { + $form = $this->createForm(CategoryType::class, $category); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->flush(); + + return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('category/edit.html.twig', [ + 'category' => $category, + 'form' => $form, + ]); + } + + #[Route('/{id}', name: 'app_category_delete', methods: ['POST'])] + public function delete(Request $request, Category $category, EntityManagerInterface $entityManager): Response + { + if ($this->isCsrfTokenValid('delete'.$category->getId(), $request->getPayload()->getString('_token'))) { + $entityManager->remove($category); + $entityManager->flush(); + } + + return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER); + } +} diff --git a/src/Form/CategoryType.php b/src/Form/CategoryType.php new file mode 100644 index 0000000..4d68103 --- /dev/null +++ b/src/Form/CategoryType.php @@ -0,0 +1,28 @@ +add('label', TextType::class, [ + 'label' => 'Category label', + ]); + + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => Category::class, + ]); + } +} diff --git a/templates/category/_delete_form.html.twig b/templates/category/_delete_form.html.twig new file mode 100644 index 0000000..535a698 --- /dev/null +++ b/templates/category/_delete_form.html.twig @@ -0,0 +1,4 @@ +
+ + +
diff --git a/templates/category/_form.html.twig b/templates/category/_form.html.twig new file mode 100644 index 0000000..bf20b98 --- /dev/null +++ b/templates/category/_form.html.twig @@ -0,0 +1,4 @@ +{{ form_start(form) }} + {{ form_widget(form) }} + +{{ form_end(form) }} diff --git a/templates/category/edit.html.twig b/templates/category/edit.html.twig new file mode 100644 index 0000000..2be3489 --- /dev/null +++ b/templates/category/edit.html.twig @@ -0,0 +1,13 @@ +{% extends 'base.html.twig' %} + +{% block title %}Edit Category{% endblock %} + +{% block body %} +

Edit Category

+ + {{ include('category/_form.html.twig', {'button_label': 'Update'}) }} + + back to list + + {{ include('category/_delete_form.html.twig') }} +{% endblock %} diff --git a/templates/category/index.html.twig b/templates/category/index.html.twig new file mode 100644 index 0000000..41949cb --- /dev/null +++ b/templates/category/index.html.twig @@ -0,0 +1,34 @@ +{% extends 'base.html.twig' %} + +{% block title %}Category index{% endblock %} + +{% block body %} +

Liste des catégories disponibles :

+ + + + + + + + + + + {% for category in categories %} + + + + + + {% else %} + + + + {% endfor %} + +
IdLabelactions
{{ category.id }}{{ category.label }} + edit +
no records found
+ + Create new +{% endblock %} diff --git a/templates/category/new.html.twig b/templates/category/new.html.twig new file mode 100644 index 0000000..bab5f3c --- /dev/null +++ b/templates/category/new.html.twig @@ -0,0 +1,11 @@ +{% extends 'base.html.twig' %} + +{% block title %}New Category{% endblock %} + +{% block body %} +

Create new Category

+ + {{ include('category/_form.html.twig') }} + + back to list +{% endblock %}