Added CRUD for domain pages

This commit is contained in:
2023-10-14 14:48:17 -06:00
parent 21178d9a1f
commit d76c0d31f9
13 changed files with 12277 additions and 1 deletions
+81
View File
@@ -0,0 +1,81 @@
<?php
namespace App\Controller;
use App\Entity\DomainPage;
use App\Form\DomainPageType;
use App\Repository\DomainPageRepository;
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('/page')]
class PageController extends AbstractController
{
#[Route('/', name: 'app_page_index', methods: ['GET'])]
public function index(DomainPageRepository $domainPageRepository): Response
{
return $this->render('page/index.html.twig', [
'domain_pages' => $domainPageRepository->findAll(),
]);
}
#[Route('/new', name: 'app_page_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
$domainPage = new DomainPage();
$form = $this->createForm(DomainPageType::class, $domainPage);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($domainPage);
$entityManager->flush();
return $this->redirectToRoute('app_page_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('page/new.html.twig', [
'domain_page' => $domainPage,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_page_show', methods: ['GET'])]
public function show(DomainPage $domainPage): Response
{
return $this->render('page/show.html.twig', [
'domain_page' => $domainPage,
]);
}
#[Route('/{id}/edit', name: 'app_page_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, DomainPage $domainPage, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(DomainPageType::class, $domainPage);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('app_page_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('page/edit.html.twig', [
'domain_page' => $domainPage,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_page_delete', methods: ['POST'])]
public function delete(Request $request, DomainPage $domainPage, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$domainPage->getId(), $request->request->get('_token'))) {
$entityManager->remove($domainPage);
$entityManager->flush();
}
return $this->redirectToRoute('app_page_index', [], Response::HTTP_SEE_OTHER);
}
}
+5
View File
@@ -56,6 +56,11 @@ class Domain
$this->domainPages = new ArrayCollection();
}
public function __toString()
{
return $this->getName() ?? $this->getId();
}
public function getId(): ?int
{
return $this->id;
+4
View File
@@ -9,6 +9,7 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
#[ORM\Entity(repositoryClass: DomainPageRepository::class)]
class DomainPage
@@ -35,9 +36,11 @@ class DomainPage
private ?string $title = null;
#[ORM\Column]
#[Gedmo\Timestampable(on: "create")]
private ?DateTimeImmutable $createdAt = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
#[Gedmo\Timestampable(on: "update")]
private ?DateTimeInterface $updatedAt = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
@@ -49,6 +52,7 @@ class DomainPage
public function __construct()
{
$this->comments = new ArrayCollection();
$this->commentCount = 0;
}
public function getId(): ?int
+42
View File
@@ -0,0 +1,42 @@
<?php
namespace App\Form;
use App\Entity\Domain;
use App\Entity\DomainPage;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class DomainPageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('locked')
->add('domain', EntityType::class, [
'class' => Domain::class,
])
->add('path', TextType::class, [
'attr' => [
'placeholder' => '/example/path'
]
])
->add('title', TextType::class, [
'attr' => [
'placeholder' => 'Example Article Name'
]
]);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => DomainPage::class,
]);
}
}