connexion base et création premier crud
This commit is contained in:
25
src/Controller/BaseController.php
Normal file
25
src/Controller/BaseController.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
/**
|
||||
* Implémente des fonctions de class "fontion répétitives"
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
abstract class BaseController extends AbstractController
|
||||
{
|
||||
protected function renderWithData(string $template,
|
||||
string $controllerName, string $msg,
|
||||
array $data): Response
|
||||
{
|
||||
return $this->render($template, [
|
||||
'controller_name' => $controllerName,
|
||||
'msg' => $msg,
|
||||
'tableau' => $data,
|
||||
]);
|
||||
}
|
||||
}
|
||||
29
src/Controller/CoucouController.php
Normal file
29
src/Controller/CoucouController.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/**
|
||||
* Affiche la page "Coucou"
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
final class CoucouController extends BaseController
|
||||
{
|
||||
#[Route('/coucou', name: 'app_coucou')]
|
||||
public function index(): Response
|
||||
{
|
||||
$tab = [
|
||||
'nom' => 'Colaboration',
|
||||
'prenom' => 'Equipe',
|
||||
];
|
||||
|
||||
return $this->renderWithData('coucou/Coucou.html.twig',
|
||||
'Faite du Sport',
|
||||
'Bien venu !',
|
||||
$tab
|
||||
);
|
||||
}
|
||||
}
|
||||
84
src/Controller/DroitController.php
Normal file
84
src/Controller/DroitController.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Droit;
|
||||
use App\Form\DroitType;
|
||||
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('/droit')]
|
||||
final class DroitController extends AbstractController
|
||||
{
|
||||
#[Route(name: 'app_droit_index', methods: ['GET'])]
|
||||
public function index(EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
$droits = $entityManager
|
||||
->getRepository(Droit::class)
|
||||
->findAll();
|
||||
|
||||
return $this->render('droit/index.html.twig', [
|
||||
'droits' => $droits,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/new', name: 'app_droit_new', methods: ['GET', 'POST'])]
|
||||
public function new(Request $request, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
$droit = new Droit();
|
||||
$form = $this->createForm(DroitType::class, $droit);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$entityManager->persist($droit);
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('app_droit_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
return $this->render('droit/new.html.twig', [
|
||||
'droit' => $droit,
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{idDroit}', name: 'app_droit_show', methods: ['GET'])]
|
||||
public function show(Droit $droit): Response
|
||||
{
|
||||
return $this->render('droit/show.html.twig', [
|
||||
'droit' => $droit,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{idDroit}/edit', name: 'app_droit_edit', methods: ['GET', 'POST'])]
|
||||
public function edit(Request $request, Droit $droit, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
$form = $this->createForm(DroitType::class, $droit);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('app_droit_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
return $this->render('droit/edit.html.twig', [
|
||||
'droit' => $droit,
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{idDroit}', name: 'app_droit_delete', methods: ['POST'])]
|
||||
public function delete(Request $request, Droit $droit, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
if ($this->isCsrfTokenValid('delete'.$droit->getIdDroit(), $request->getPayload()->getString('_token'))) {
|
||||
$entityManager->remove($droit);
|
||||
$entityManager->flush();
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('app_droit_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
}
|
||||
84
src/Controller/MembreController.php
Normal file
84
src/Controller/MembreController.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Membre;
|
||||
use App\Form\MembreType;
|
||||
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('/membre')]
|
||||
final class MembreController extends AbstractController
|
||||
{
|
||||
#[Route(name: 'app_membre_index', methods: ['GET'])]
|
||||
public function index(EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
$membres = $entityManager
|
||||
->getRepository(Membre::class)
|
||||
->findAll();
|
||||
|
||||
return $this->render('membre/index.html.twig', [
|
||||
'membres' => $membres,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/new', name: 'app_membre_new', methods: ['GET', 'POST'])]
|
||||
public function new(Request $request, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
$membre = new Membre();
|
||||
$form = $this->createForm(MembreType::class, $membre);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$entityManager->persist($membre);
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('app_membre_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
return $this->render('membre/new.html.twig', [
|
||||
'membre' => $membre,
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'app_membre_show', methods: ['GET'])]
|
||||
public function show(Membre $membre): Response
|
||||
{
|
||||
return $this->render('membre/show.html.twig', [
|
||||
'membre' => $membre,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{id}/edit', name: 'app_membre_edit', methods: ['GET', 'POST'])]
|
||||
public function edit(Request $request, Membre $membre, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
$form = $this->createForm(MembreType::class, $membre);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('app_membre_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
return $this->render('membre/edit.html.twig', [
|
||||
'membre' => $membre,
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'app_membre_delete', methods: ['POST'])]
|
||||
public function delete(Request $request, Membre $membre, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
if ($this->isCsrfTokenValid('delete'.$membre->getId(), $request->getPayload()->getString('_token'))) {
|
||||
$entityManager->remove($membre);
|
||||
$entityManager->flush();
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('app_membre_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
}
|
||||
84
src/Controller/OController.php
Normal file
84
src/Controller/OController.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Membre;
|
||||
use App\Form\Membre1Type;
|
||||
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('/o')]
|
||||
final class OController extends AbstractController
|
||||
{
|
||||
#[Route(name: 'app_o_index', methods: ['GET'])]
|
||||
public function index(EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
$membres = $entityManager
|
||||
->getRepository(Membre::class)
|
||||
->findAll();
|
||||
|
||||
return $this->render('o/index.html.twig', [
|
||||
'membres' => $membres,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/new', name: 'app_o_new', methods: ['GET', 'POST'])]
|
||||
public function new(Request $request, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
$membre = new Membre();
|
||||
$form = $this->createForm(Membre1Type::class, $membre);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$entityManager->persist($membre);
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('app_o_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
return $this->render('o/new.html.twig', [
|
||||
'membre' => $membre,
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'app_o_show', methods: ['GET'])]
|
||||
public function show(Membre $membre): Response
|
||||
{
|
||||
return $this->render('o/show.html.twig', [
|
||||
'membre' => $membre,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{id}/edit', name: 'app_o_edit', methods: ['GET', 'POST'])]
|
||||
public function edit(Request $request, Membre $membre, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
$form = $this->createForm(Membre1Type::class, $membre);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('app_o_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
return $this->render('o/edit.html.twig', [
|
||||
'membre' => $membre,
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'app_o_delete', methods: ['POST'])]
|
||||
public function delete(Request $request, Membre $membre, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
if ($this->isCsrfTokenValid('delete'.$membre->getId(), $request->getPayload()->getString('_token'))) {
|
||||
$entityManager->remove($membre);
|
||||
$entityManager->flush();
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('app_o_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
}
|
||||
22
src/Controller/PecheController.php
Normal file
22
src/Controller/PecheController.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
/**
|
||||
* Affiche la page "Peche"
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
final class PecheController extends BaseController
|
||||
{
|
||||
#[Route('/peche', name: 'app_peche')]
|
||||
public function index(): Response
|
||||
{
|
||||
return $this->render('peche/index.html.twig', [
|
||||
'controller_name' => 'PecheController',
|
||||
]);
|
||||
}
|
||||
}
|
||||
29
src/Controller/SportController.php
Normal file
29
src/Controller/SportController.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/**
|
||||
* Affiche la page "Sport"
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
final class SportController extends BaseController
|
||||
{
|
||||
#[Route('/sport', name: 'app_sport')]
|
||||
public function sport(): Response
|
||||
{
|
||||
$tab = [
|
||||
'categorie' => 'Sport-Catégorie',
|
||||
'specialite' => 'Spécialisation',
|
||||
];
|
||||
|
||||
return $this->renderWithData('sport/sport.html.twig',
|
||||
'Faire du Sport',
|
||||
'Venez avec nous !',
|
||||
$tab
|
||||
);
|
||||
}
|
||||
}
|
||||
35
src/Entity/Droit.php
Normal file
35
src/Entity/Droit.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
// src/Entity/Droit.php
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity]
|
||||
#[ORM\Table(name: "Droit")]
|
||||
class Droit
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: "integer", name: "idDroit")]
|
||||
private ?int $idDroit = null;
|
||||
|
||||
#[ORM\Column(type: "string", length: 30, name: "LibDroit")]
|
||||
private ?string $libDroit = null;
|
||||
|
||||
// Getter et Setter
|
||||
public function getIdDroit(): ?int
|
||||
{
|
||||
return $this->idDroit;
|
||||
}
|
||||
|
||||
public function getLibDroit(): ?string
|
||||
{
|
||||
return $this->libDroit;
|
||||
}
|
||||
|
||||
public function setLibDroit(string $libDroit): self
|
||||
{
|
||||
$this->libDroit = $libDroit;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
37
src/Entity/Membre.php
Normal file
37
src/Entity/Membre.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
// src/Entity/Membre.php
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity]
|
||||
#[ORM\Table(name: "Membre")]
|
||||
class Membre
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\Column(type: "string", length: 20)]
|
||||
private ?string $id = null;
|
||||
|
||||
#[ORM\Column(type: "string", length: 50)]
|
||||
private ?string $nom = null;
|
||||
|
||||
#[ORM\Column(type: "string", length: 100, nullable: true)]
|
||||
private ?string $password = null;
|
||||
|
||||
#[ORM\ManyToOne(targetEntity: Droit::class)]
|
||||
#[ORM\JoinColumn(name: "droit_id", referencedColumnName: "idDroit", nullable: true, onDelete: "SET NULL")]
|
||||
private ?Droit $droit = null;
|
||||
|
||||
// Getters et setters
|
||||
public function getId(): ?string { return $this->id; }
|
||||
public function setId(string $id): self { $this->id = $id; return $this; }
|
||||
|
||||
public function getNom(): ?string { return $this->nom; }
|
||||
public function setNom(string $nom): self { $this->nom = $nom; return $this; }
|
||||
|
||||
public function getPassword(): ?string { return $this->password; }
|
||||
public function setPassword(?string $password): self { $this->password = $password; return $this; }
|
||||
|
||||
public function getDroit(): ?Droit { return $this->droit; }
|
||||
public function setDroit(?Droit $droit): self { $this->droit = $droit; return $this; }
|
||||
}
|
||||
25
src/Form/DroitType.php
Normal file
25
src/Form/DroitType.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Entity\Droit;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class DroitType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
->add('libDroit')
|
||||
;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => Droit::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
33
src/Form/Membre1Type.php
Normal file
33
src/Form/Membre1Type.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Entity\Droit;
|
||||
use App\Entity\Membre;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class Membre1Type extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
->add('id')
|
||||
->add('nom')
|
||||
->add('password')
|
||||
->add('droit', EntityType::class, [
|
||||
'class' => Droit::class,
|
||||
'choice_label' => 'id',
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => Membre::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
33
src/Form/MembreType.php
Normal file
33
src/Form/MembreType.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Entity\Droit;
|
||||
use App\Entity\Membre;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class MembreType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
->add('id')
|
||||
->add('nom')
|
||||
->add('password')
|
||||
->add('droit', EntityType::class, [
|
||||
'class' => Droit::class,
|
||||
'choice_label' => 'id',
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => Membre::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
15
src/Repository/MembreRepository.php
Normal file
15
src/Repository/MembreRepository.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
// src/Repository/MembreRepository.php
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\Membre;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
class MembreRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Membre::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user