Initial commit
This commit is contained in:
85
src/Controller/MonsterCategoryController.php
Normal file
85
src/Controller/MonsterCategoryController.php
Normal file
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\MonsterCategory;
|
||||
use App\Repository\MonsterCategoryRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
#[Route('/dashboard/monsters-categories')]
|
||||
class MonsterCategoryController extends AbstractController {
|
||||
public function __construct(private EntityManagerInterface $_em,
|
||||
private MonsterCategoryRepository $monsterCategoryRepository) {}
|
||||
|
||||
#[Route('', name: 'bo_monster_category_index', methods: ['GET'])]
|
||||
public function index(): Response {
|
||||
$monstersCategories = $this->monsterCategoryRepository->findBy(array(), array('name' => 'ASC'));
|
||||
|
||||
return $this->render('_dashboard/monster_category/index.html.twig', array(
|
||||
'monstersCategories' => $monstersCategories,
|
||||
));
|
||||
}
|
||||
|
||||
#[Route('/new', name: 'bo_monster_category_new', methods: ['GET', 'POST'])]
|
||||
public function new(Request $request): RedirectResponse|Response {
|
||||
$this->denyAccessUnlessGranted('ROLE_ADMIN');
|
||||
|
||||
$monstersCategories = $this->monsterCategoryRepository->findBy(array(), array('name' => 'ASC'));
|
||||
|
||||
$monsterCategory = new MonsterCategory();
|
||||
$form = $this->monsterCategoryRepository->getForm('bo_monster_category_new', $monsterCategory);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if($form->isSubmitted() && $form->isValid()) {
|
||||
$this->_em->persist($monsterCategory);
|
||||
$this->_em->flush();
|
||||
|
||||
return $this->redirectToRoute('bo_monster_category_index', array(), Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
return $this->renderForm('_dashboard/monster_category/new.html.twig', array(
|
||||
'monstersCategories' => $monstersCategories,
|
||||
'monsterCategory' => $monsterCategory,
|
||||
'form' => $form,
|
||||
));
|
||||
}
|
||||
|
||||
#[Route('/category-{id}', name: 'bo_monster_category_edit', methods: ['GET', 'POST'])]
|
||||
public function edit(Request $request, MonsterCategory $monsterCategory): RedirectResponse|Response {
|
||||
$this->denyAccessUnlessGranted('ROLE_ADMIN');
|
||||
|
||||
$monstersCategories = $this->monsterCategoryRepository->findBy(array(), array('name' => 'ASC'));
|
||||
|
||||
$form = $this->monsterCategoryRepository->getForm('bo_monster_category_edit', $monsterCategory);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if($form->isSubmitted() && $form->isValid()) {
|
||||
$this->_em->flush();
|
||||
|
||||
return $this->redirectToRoute('bo_monster_category_index', array(), Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
return $this->renderForm('_dashboard/monster_category/edit.html.twig', array(
|
||||
'monstersCategories' => $monstersCategories,
|
||||
'monsterCategory' => $monsterCategory,
|
||||
'form' => $form,
|
||||
));
|
||||
}
|
||||
|
||||
#[Route('/delete-{id}', name: 'bo_monster_category_delete', methods: ['POST'])]
|
||||
public function delete(Request $request, MonsterCategory $monsterCategory): RedirectResponse {
|
||||
$this->denyAccessUnlessGranted('ROLE_ADMIN');
|
||||
|
||||
if($this->isCsrfTokenValid('delete'.$monsterCategory->getId(), $request->request->get('_token'))) {
|
||||
$this->_em->remove($monsterCategory);
|
||||
$this->_em->flush();
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('bo_monster_category_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user