Created
November 13, 2017 15:09
-
-
Save ollo-ride-nico/ae16163f4ba6543f044a0c72afbe5a96 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TrickController extends Controller | |
{ | |
private $builder; | |
public function __construct(TricksBuilder $builder) | |
{ | |
$this->builder = $builder; | |
} | |
public function createTrickAction(Request $request) | |
{ | |
// On instancie l' objet Tricks avec des valeurs par défaut | |
$tricks = $this->builder | |
->create() | |
->withNom('votre nom ici') | |
->build(); | |
//On crée le formulaire | |
$form = $this->createForm(TricksType::class, $tricks); | |
$form->handleRequest($request); | |
if ($form->isSubmitted() && $form->isValid()) { | |
$tricks = $form->getData(); | |
// Ajout du tricks en base de donnée | |
$em = $this->getDoctrine()->getManager(); | |
$em->persist($tricks); | |
$em->flush(); | |
return $this->render('confirm.html.twig', array('text' => 'Trick créé')); | |
} | |
return $this->render('formTrick.html.twig', array('form' => $form->createView(), | |
)); | |
} | |
public function getTrickAction($id) | |
{ | |
$trick = $this->getDoctrine() | |
->getManager() | |
->getRepository('App:Tricks') | |
->find($id); | |
return $this->render('trick.html.twig', array('trick' => $trick)); | |
} | |
public function getListTrickAction() | |
{ | |
$repository = $this | |
->getDoctrine() | |
->getManager() | |
->getRepository(Tricks::class); | |
$listTricks = $repository->findAll(); | |
return $this->render('index.html.twig', array('listTricks' => $listTricks)); | |
} | |
public function updateTrickAction($id, Request $request) | |
{ | |
$updateTrick = $this->getDoctrine() | |
->getManager() | |
->getRepository('App:Tricks') | |
->find($id); | |
// Date de modification | |
$updateTrick->setDateModification(new \DateTime()); | |
$form = $this->createForm(TricksType::class, $updateTrick); | |
$form->handleRequest($request); | |
if ($form->isSubmitted() && $form->isValid()) { | |
$updateTrick = $form->getData(); | |
// Ajout du trick en base de donnée | |
$em = $this->getDoctrine()->getManager(); | |
$em->persist($updateTrick); | |
$em->flush(); | |
return $this->render('confirm.html.twig', array('text' => 'Trick créé')); | |
} | |
return $this->render('formTrick.html.twig', array('form' => $form->createView(), | |
)); | |
} | |
public function deleteTrickAction($id, Request $request) | |
{ | |
$trick = $this->getDoctrine() | |
->getManager() | |
->getRepository('App:Tricks') | |
->find($id); | |
$form = $this->createForm(TricksDeleteType::class, $trick); | |
$form->handleRequest($request); | |
if ($form->isSubmitted() && $form->isValid()) { | |
// Suppression du trick en base de donnée | |
$em = $this->getDoctrine()->getManager(); | |
$em->remove($trick); | |
$em->flush(); | |
return $this->render('confirm.html.twig', array('text' => 'Trick delete')); | |
} | |
return $this->render('formTrick.html.twig', array('form' => $form->createView() | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment