Created
January 20, 2015 20:38
-
-
Save AndrianD/42ffa8f609991a794c96 to your computer and use it in GitHub Desktop.
Andriana\TchoupiBundle\ToysController.php Symfony2
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
<?php | |
namespace Andriana\TchoupiBundle\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 Andriana\TchoupiBundle\Entity\Toys; | |
use Andriana\TchoupiBundle\Form\ToysType; | |
use Symfony\Component\HttpFoundation\Response; | |
/** | |
* Toys controller. | |
* | |
* @Route("/toys") | |
*/ | |
class ToysController extends Controller | |
{ | |
/** | |
* Lists all Toys entities. | |
* | |
* @Route("/", name="toys") | |
* @Method("GET") | |
* @Template() | |
*/ | |
public function indexAction() | |
{ | |
$em = $this->getDoctrine()->getManager(); | |
$entities = $em->getRepository('AndrianaTchoupiBundle:Toys')->findAll(); | |
return array( | |
'entities' => $entities, | |
); | |
} | |
/** | |
* Creates a new Toys entity. | |
* | |
* @Route("/", name="toys_create") | |
* @Method("POST") | |
* @Template("AndrianaTchoupiBundle:Toys:new.html.twig") | |
*/ | |
public function createAction(Request $request) | |
{ | |
$entity = new Toys(); | |
$form = $this->createCreateForm($entity); | |
$form->handleRequest($request); | |
if ($form->isValid()) { | |
$em = $this->getDoctrine()->getManager(); | |
$em->persist($entity); | |
$em->flush(); | |
return $this->redirect($this->generateUrl('toys_show', array('id' => $entity->getId()))); | |
} | |
return array( | |
'entity' => $entity, | |
'form' => $form->createView(), | |
); | |
} | |
/** | |
* Creates a form to create a Toys entity. | |
* | |
* @param Toys $entity The entity | |
* | |
* @return \Symfony\Component\Form\Form The form | |
*/ | |
private function createCreateForm(Toys $entity) | |
{ | |
$form = $this->createForm(new ToysType(), $entity, array( | |
'action' => $this->generateUrl('toys_create'), | |
'method' => 'POST', | |
)); | |
$form->add('submit', 'submit', array('label' => 'Create')); | |
return $form; | |
} | |
/** | |
* Displays a form to create a new Toys entity. | |
* | |
* @Route("/new", name="toys_new") | |
* @Method("GET") | |
* @Template() | |
*/ | |
public function newAction() | |
{ | |
$entity = new Toys(); | |
$form = $this->createCreateForm($entity); | |
return array( | |
'entity' => $entity, | |
'form' => $form->createView(), | |
); | |
} | |
/** | |
* Finds and displays a Toys entity. | |
* | |
* @Route("/{id}", name="toys_show") | |
* @Method("GET") | |
* @Template() | |
*/ | |
public function showAction($id) | |
{ | |
$em = $this->getDoctrine()->getManager(); | |
$entity = $em->getRepository('AndrianaTchoupiBundle:Toys')->find($id); | |
if (!$entity) { | |
throw $this->createNotFoundException('Unable to find Toys entity.'); | |
} | |
$deleteForm = $this->createDeleteForm($id); | |
return array( | |
'entity' => $entity, | |
'delete_form' => $deleteForm->createView(), | |
); | |
} | |
/** | |
* Displays a form to edit an existing Toys entity. | |
* | |
* @Route("/{id}/edit", name="toys_edit") | |
* @Method("GET") | |
* @Template() | |
*/ | |
public function editAction($id) | |
{ | |
$em = $this->getDoctrine()->getManager(); | |
$entity = $em->getRepository('AndrianaTchoupiBundle:Toys')->find($id); | |
if (!$entity) { | |
throw $this->createNotFoundException('Unable to find Toys 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 Toys entity. | |
* | |
* @param Toys $entity The entity | |
* | |
* @return \Symfony\Component\Form\Form The form | |
*/ | |
private function createEditForm(Toys $entity) | |
{ | |
$form = $this->createForm(new ToysType(), $entity, array( | |
'action' => $this->generateUrl('toys_update', array('id' => $entity->getId())), | |
'method' => 'PUT', | |
)); | |
$form->add('submit', 'submit', array('label' => 'Update')); | |
return $form; | |
} | |
/** | |
* Edits an existing Toys entity. | |
* | |
* @Route("/{id}", name="toys_update") | |
* @Method("PUT") | |
* @Template("AndrianaTchoupiBundle:Toys:edit.html.twig") | |
*/ | |
public function updateAction(Request $request, $id) | |
{ | |
$em = $this->getDoctrine()->getManager(); | |
$entity = $em->getRepository('AndrianaTchoupiBundle:Toys')->find($id); | |
if (!$entity) { | |
throw $this->createNotFoundException('Unable to find Toys entity.'); | |
} | |
$deleteForm = $this->createDeleteForm($id); | |
$editForm = $this->createEditForm($entity); | |
$editForm->handleRequest($request); | |
if ($editForm->isValid()) { | |
$em->flush(); | |
return $this->redirect($this->generateUrl('toys_edit', array('id' => $id))); | |
} | |
return array( | |
'entity' => $entity, | |
'edit_form' => $editForm->createView(), | |
'delete_form' => $deleteForm->createView(), | |
); | |
} | |
/** | |
* Deletes a Toys entity. | |
* | |
* @Route("/{id}", name="toys_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('AndrianaTchoupiBundle:Toys')->find($id); | |
if (!$entity) { | |
throw $this->createNotFoundException('Unable to find Toys entity.'); | |
} | |
$em->remove($entity); | |
$em->flush(); | |
} | |
return $this->redirect($this->generateUrl('toys')); | |
} | |
/** | |
* Creates a form to delete a Toys 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('toys_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