collection-crud/src/Controller/BrandCategoryController.php

85 lines
3.0 KiB
PHP

<?php
namespace App\Controller;
use App\Entity\BrandCategory;
use App\Form\BrandCategoryType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/brand/category')]
class BrandCategoryController extends AbstractController
{
#[Route('/', name: 'brand-category_index', methods: ['GET'])]
public function index(EntityManagerInterface $entityManager): Response
{
$brandCategories = $entityManager
->getRepository(BrandCategory::class)
->findAll();
return $this->render('brand_category/index.html.twig', [
'brand_categories' => $brandCategories,
]);
}
#[Route('/new', name: 'brand-category_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
$brandCategory = new BrandCategory();
$form = $this->createForm(BrandCategoryType::class, $brandCategory);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($brandCategory);
$entityManager->flush();
return $this->redirectToRoute('brand-category_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('brand_category/new.html.twig', [
'brand_category' => $brandCategory,
'form' => $form,
]);
}
#[Route('/{name}', name: 'brand-category_show', methods: ['GET'])]
public function show(BrandCategory $brandCategory): Response
{
return $this->render('brand_category/show.html.twig', [
'brand_category' => $brandCategory,
]);
}
#[Route('/{name}/edit', name: 'brand-category_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, BrandCategory $brandCategory, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(BrandCategoryType::class, $brandCategory);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('brand-category_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('brand_category/edit.html.twig', [
'brand_category' => $brandCategory,
'form' => $form,
]);
}
#[Route('/{name}', name: 'brand-category_delete', methods: ['POST'])]
public function delete(Request $request, BrandCategory $brandCategory, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$brandCategory->getName(), $request->request->get('_token'))) {
$entityManager->remove($brandCategory);
$entityManager->flush();
}
return $this->redirectToRoute('brand_category_index', [], Response::HTTP_SEE_OTHER);
}
}