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 %} +Id | +Label | +actions | +
---|---|---|
{{ category.id }} | +{{ category.label }} | ++ edit + | +
no records found | +