Created
November 8, 2018 23:12
-
-
Save andrechavesg/00fcdb2822ad40b73c3ef597f869a753 to your computer and use it in GitHub Desktop.
This file contains 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
<?php | |
namespace App\Controller; | |
use App\Entity\Livro; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\Routing\Annotation\Route; | |
class LivroController extends Controller | |
{ | |
/** | |
* @Route("/livro/mostra",methods="GET") | |
*/ | |
public function mostraAction() | |
{ | |
return $this->render('Livro/mostra.html.twig',["livro" => new Livro()]); | |
} | |
/** | |
* @Route("/livro/novo",methods="GET") | |
*/ | |
public function formulario() | |
{ | |
$form = $this->createFormBuilder(new Livro()) | |
->add('nome') | |
->add('autor') | |
->setAction('/livro/novo') | |
->getForm(); | |
return $this->render("Livro/novo.html.twig",["form" => $form->createView()]); | |
} | |
/** | |
* @Route("/livro/novo",methods="POST") | |
*/ | |
public function cria(Request $request) | |
{ | |
$livro = new Livro(); | |
$form = $this->createFormBuilder($livro) | |
->add('nome') | |
->add('autor') | |
->getForm(); | |
$form->handleRequest($request); | |
$em = $this->getDoctrine()->getManager(); | |
$em->persist($livro); | |
$em->flush(); | |
return $this->redirect("/livro/lista"); | |
} | |
/** | |
* @Route("/livro/lista",methods="GET") | |
*/ | |
public function lista() | |
{ | |
$repository = $this->getDoctrine()->getManager()->getRepository(Livro::class); | |
return $this->render("Livro/lista.html.twig",["livros" => $repository->findAll()]); | |
} | |
/** | |
* @Route("/livro/edita/{id}",methods="GET") | |
*/ | |
public function mostra(Livro $livro) | |
{ | |
$form = $this->createFormBuilder($livro) | |
->add('nome') | |
->add('autor') | |
->getForm(); | |
return $this->render('Livro/edita.html.twig',["livro" => $livro,"form" => $form->createView()]); | |
} | |
/** | |
* @Route("/livro/edita/{id}",methods="POST") | |
*/ | |
public function edita(Livro $livro, Request $request) | |
{ | |
$form = $this->createFormBuilder($livro) | |
->add('nome') | |
->add('autor') | |
->getForm(); | |
$form->handleRequest($request); | |
if ($form->isValid()) { | |
$livro = $form->getData(); | |
$em = $this->getDoctrine()->getManager(); | |
$em->merge($livro); | |
$em->flush(); | |
return $this->redirect("/livro/edita/".$livro->getId()); | |
} | |
return $this->render('Livro/edita.html.twig',["livro" => $livro]); | |
} | |
/** | |
* @Route("/livro/remove/{id}",methods="GET") | |
*/ | |
public function delete(Livro $livro) | |
{ | |
$em = $this->getDoctrine()->getManager(); | |
$em->remove($livro); | |
$em->flush(); | |
return $this->redirect("/livro/lista"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment