Add post and species forms
Squashed commit of the following: Author: Hugo PRADIER <Hugo.PRADIER2@etu.uca.fr> Author: bastien ollier <bastien.ollier@etu.uca.fr> Author: clfreville2 <clement.freville2@etu.uca.fr> Reviewed on #7
This commit is contained in:
@@ -2,19 +2,85 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Post;
|
||||
use App\Form\PostType;
|
||||
use App\Repository\PostRepository;
|
||||
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;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
class PostController extends AbstractController
|
||||
{
|
||||
#[Route('/', name: 'app_posts')]
|
||||
#[Route('/post', name: 'app_post_index', methods: ['GET'])]
|
||||
public function index(PostRepository $repository): Response
|
||||
{
|
||||
$posts = $repository->findAll();
|
||||
return $this->render('post/index.html.twig', [
|
||||
return $this->render('post/table.html.twig', [
|
||||
'posts' => $posts,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/post/new', name: 'app_post_new', methods: ['GET', 'POST'])]
|
||||
#[IsGranted('ROLE_USER')]
|
||||
public function new(Request $request, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
$post = new Post();
|
||||
$form = $this->createForm(PostType::class, $post);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$entityManager->persist($post);
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('app_posts', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
return $this->render('post/new.html.twig', [
|
||||
'post' => $post,
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/post/{id}', name: 'app_post_show', methods: ['GET'])]
|
||||
public function show(Post $post): Response
|
||||
{
|
||||
return $this->render('post/show.html.twig', [
|
||||
'post' => $post,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/post/{id}/edit', name: 'app_post_edit', methods: ['GET', 'POST'])]
|
||||
#[IsGranted('ROLE_USER')]
|
||||
public function edit(Request $request, Post $post, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
$form = $this->createForm(PostType::class, $post);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('app_posts', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
return $this->render('post/edit.html.twig', [
|
||||
'post' => $post,
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/post/{id}', name: 'app_post_delete', methods: ['POST'])]
|
||||
#[IsGranted('ROLE_USER')]
|
||||
public function delete(Request $request, Post $post, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
if ($this->isCsrfTokenValid('delete'.$post->getId(), (string) $request->getPayload()->get('_token'))) {
|
||||
$entityManager->remove($post);
|
||||
$entityManager->flush();
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('app_posts', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
}
|
||||
|
@@ -34,7 +34,11 @@ class RegistrationController extends AbstractController
|
||||
|
||||
// do anything else you need here, like send an email
|
||||
|
||||
return $this->redirectToRoute('_profiler_home');
|
||||
return $this->redirectToRoute('app_login');
|
||||
}
|
||||
|
||||
if ($this->getUser()) {
|
||||
return $this->redirectToRoute('app_posts');
|
||||
}
|
||||
|
||||
return $this->render('registration/register.html.twig', [
|
||||
|
@@ -3,27 +3,83 @@
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Species;
|
||||
use App\Form\SpeciesType;
|
||||
use App\Repository\SpeciesRepository;
|
||||
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;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
#[Route('/species')]
|
||||
class SpeciesController extends AbstractController
|
||||
{
|
||||
#[Route('/species', name: 'app_species')]
|
||||
public function index(SpeciesRepository $repository): Response
|
||||
#[Route('/', name: 'app_species_index', methods: ['GET'])]
|
||||
public function table(SpeciesRepository $speciesRepository): Response
|
||||
{
|
||||
$species = $repository->findAll();
|
||||
return $this->render('species/index.html.twig', [
|
||||
return $this->render('species/table.html.twig', [
|
||||
'species' => $speciesRepository->findAll(),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/new', name: 'app_species_new', methods: ['GET', 'POST'])]
|
||||
#[IsGranted('ROLE_USER')]
|
||||
public function new(Request $request, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
$species = new Species();
|
||||
$form = $this->createForm(SpeciesType::class, $species);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$entityManager->persist($species);
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('app_species_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
return $this->render('species/new.html.twig', [
|
||||
'species' => $species,
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'app_species_show', methods: ['GET'])]
|
||||
public function show(Species $species): Response
|
||||
{
|
||||
return $this->render('species/show.html.twig', [
|
||||
'species' => $species,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/species/{id}', name: 'app_species_detail')]
|
||||
public function detail(Species $specie): Response
|
||||
#[Route('/{id}/edit', name: 'app_species_edit', methods: ['GET', 'POST'])]
|
||||
#[IsGranted('ROLE_USER')]
|
||||
public function edit(Request $request, Species $species, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
return $this->render('species/detail.html.twig', [
|
||||
'specie' => $specie,
|
||||
$form = $this->createForm(SpeciesType::class, $species);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('app_species_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
return $this->render('species/edit.html.twig', [
|
||||
'species' => $species,
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'app_species_delete', methods: ['POST'])]
|
||||
#[IsGranted('ROLE_USER')]
|
||||
public function delete(Request $request, Species $species, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
if ($this->isCsrfTokenValid('delete'.$species->getId(), (string) $request->getPayload()->get('_token'))) {
|
||||
$entityManager->remove($species);
|
||||
$entityManager->flush();
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('app_species_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user