Skip to content

Instantly share code, notes, and snippets.

@maximecolin
Created November 27, 2013 10:26
Show Gist options
  • Save maximecolin/7673587 to your computer and use it in GitHub Desktop.
Save maximecolin/7673587 to your computer and use it in GitHub Desktop.
<?php
namespace Colin\Bundle\TotoBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Colin\Bundle\TotoBundle\Entity\Toto;
use Colin\Bundle\TotoBundle\Form\TotoType;
/**
* Toto controller.
*
* @Route("/toto")
*/
class TotoController extends Controller
{
/**
* Lists all Toto entities.
*
* @Route("/", name="toto")
* @Method("GET")
* @Template()
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('ColinTotoBundle:Toto')->findAll();
return array(
'entities' => $entities,
);
}
/**
* Creates a new Toto entity.
*
* @Route("/", name="toto_create")
* @Method("POST")
* @Template("ColinTotoBundle:Toto:new.html.twig")
*/
public function createAction(Request $request)
{
$entity = new Toto();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('toto_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Creates a form to create a Toto entity.
*
* @param Toto $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(Toto $entity)
{
$form = $this->createForm(new TotoType(), $entity, array(
'action' => $this->generateUrl('toto_create'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
/**
* Displays a form to create a new Toto entity.
*
* @Route("/new", name="toto_new")
* @Method("GET")
* @Template()
*/
public function newAction()
{
$entity = new Toto();
$form = $this->createCreateForm($entity);
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Finds and displays a Toto entity.
*
* @Route("/{id}", name="toto_show")
* @Method("GET")
* @Template()
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ColinTotoBundle:Toto')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Toto entity.');
}
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
);
}
/**
* Displays a form to edit an existing Toto entity.
*
* @Route("/{id}/edit", name="toto_edit")
* @Method("GET")
* @Template()
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ColinTotoBundle:Toto')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Toto entity.');
}
$editForm = $this->createEditForm($entity);
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Creates a form to edit a Toto entity.
*
* @param Toto $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(Toto $entity)
{
$form = $this->createForm(new TotoType(), $entity, array(
'action' => $this->generateUrl('toto_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Update'));
return $form;
}
/**
* Edits an existing Toto entity.
*
* @Route("/{id}", name="toto_update")
* @Method("PUT")
* @Template("ColinTotoBundle:Toto:edit.html.twig")
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ColinTotoBundle:Toto')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Toto entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->flush();
return $this->redirect($this->generateUrl('toto_edit', array('id' => $id)));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Deletes a Toto entity.
*
* @Route("/{id}", name="toto_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ColinTotoBundle:Toto')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Toto entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('toto'));
}
/**
* Creates a form to delete a Toto entity by id.
*
* @param mixed $id The entity id
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm($id)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('toto_delete', array('id' => $id)))
->setMethod('DELETE')
->add('submit', 'submit', array('label' => 'Delete'))
->getForm()
;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment