add api platform to entitites

This commit is contained in:
Tykayn 2025-02-14 14:35:21 +01:00 committed by tykayn
parent c44ac9c522
commit 9c9f8984af
15 changed files with 13 additions and 369 deletions

View file

@ -1,81 +0,0 @@
<?php
namespace App\Controller;
use App\Entity\Category;
use App\Form\CategoryType;
use App\Repository\CategoryRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/category')]
final class CategoryController extends AbstractController
{
#[Route(name: 'app_category_index', methods: ['GET'])]
public function index(CategoryRepository $categoryRepository): Response
{
return $this->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}', name: 'app_category_show', methods: ['GET'])]
public function show(Category $category): Response
{
return $this->render('category/show.html.twig', [
'category' => $category,
]);
}
#[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);
}
}