Created
September 3, 2014 16:27
-
-
Save bactisme/0d0e1f939ffe02699def 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 Sidereo\CoreBundle\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 Sidereo\CoreBundle\Entity\App; | |
use Sidereo\CoreBundle\Form\AppType; | |
/** | |
* App controller. | |
* | |
* @Route("/manage/app/") | |
*/ | |
class AppController extends Controller | |
{ | |
/** | |
* Lists all App entities. | |
* | |
* @Route("/", name="manage_app_") | |
* @Method("GET") | |
* @Template() | |
*/ | |
public function indexAction() | |
{ | |
$em = $this->getDoctrine()->getManager(); | |
$entities = $em->getRepository('SidereoCoreBundle:App')->findAll(); | |
return array( | |
'entities' => $entities, | |
); | |
} | |
/** | |
* Creates a new App entity. | |
* | |
* @Route("/", name="manage_app__create") | |
* @Method("POST") | |
* @Template("SidereoCoreBundle:App:new.html.twig") | |
*/ | |
public function createAction(Request $request) | |
{ | |
$entity = new App(); | |
$form = $this->createCreateForm($entity); | |
$form->handleRequest($request); | |
if ($form->isValid()) { | |
$em = $this->getDoctrine()->getManager(); | |
$em->persist($entity); | |
$em->flush(); | |
return $this->redirect($this->generateUrl('manage_app__show', array('id' => $entity->getId()))); | |
} | |
return array( | |
'entity' => $entity, | |
'form' => $form->createView(), | |
); | |
} | |
/** | |
* Creates a form to create a App entity. | |
* | |
* @param App $entity The entity | |
* | |
* @return \Symfony\Component\Form\Form The form | |
*/ | |
private function createCreateForm(App $entity) | |
{ | |
$form = $this->createForm(new AppType(), $entity, array( | |
'action' => $this->generateUrl('manage_app__create'), | |
'method' => 'POST', | |
)); | |
$form->add('submit', 'submit', array('label' => 'Create')); | |
return $form; | |
} | |
/** | |
* Displays a form to create a new App entity. | |
* | |
* @Route("/new", name="manage_app__new") | |
* @Method("GET") | |
* @Template() | |
*/ | |
public function newAction() | |
{ | |
$entity = new App(); | |
$form = $this->createCreateForm($entity); | |
return array( | |
'entity' => $entity, | |
'form' => $form->createView(), | |
); | |
} | |
/** | |
* Finds and displays a App entity. | |
* | |
* @Route("/{id}", name="manage_app__show") | |
* @Method("GET") | |
* @Template() | |
*/ | |
public function showAction($id) | |
{ | |
$em = $this->getDoctrine()->getManager(); | |
$entity = $em->getRepository('SidereoCoreBundle:App')->find($id); | |
if (!$entity) { | |
throw $this->createNotFoundException('Unable to find App entity.'); | |
} | |
$deleteForm = $this->createDeleteForm($id); | |
return array( | |
'entity' => $entity, | |
'delete_form' => $deleteForm->createView(), | |
); | |
} | |
/** | |
* Displays a form to edit an existing App entity. | |
* | |
* @Route("/{id}/edit", name="manage_app__edit") | |
* @Method("GET") | |
* @Template() | |
*/ | |
public function editAction($id) | |
{ | |
$em = $this->getDoctrine()->getManager(); | |
$entity = $em->getRepository('SidereoCoreBundle:App')->find($id); | |
if (!$entity) { | |
throw $this->createNotFoundException('Unable to find App 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 App entity. | |
* | |
* @param App $entity The entity | |
* | |
* @return \Symfony\Component\Form\Form The form | |
*/ | |
private function createEditForm(App $entity) | |
{ | |
$form = $this->createForm(new AppType(), $entity, array( | |
'action' => $this->generateUrl('manage_app__update', array('id' => $entity->getId())), | |
'method' => 'PUT', | |
)); | |
$form->add('submit', 'submit', array('label' => 'Update')); | |
return $form; | |
} | |
/** | |
* Edits an existing App entity. | |
* | |
* @Route("/{id}", name="manage_app__update") | |
* @Method("PUT") | |
* @Template("SidereoCoreBundle:App:edit.html.twig") | |
*/ | |
public function updateAction(Request $request, $id) | |
{ | |
$em = $this->getDoctrine()->getManager(); | |
$entity = $em->getRepository('SidereoCoreBundle:App')->find($id); | |
if (!$entity) { | |
throw $this->createNotFoundException('Unable to find App entity.'); | |
} | |
$deleteForm = $this->createDeleteForm($id); | |
$editForm = $this->createEditForm($entity); | |
$editForm->handleRequest($request); | |
if ($editForm->isValid()) { | |
$em->flush(); | |
return $this->redirect($this->generateUrl('manage_app__edit', array('id' => $id))); | |
} | |
return array( | |
'entity' => $entity, | |
'edit_form' => $editForm->createView(), | |
'delete_form' => $deleteForm->createView(), | |
); | |
} | |
/** | |
* Deletes a App entity. | |
* | |
* @Route("/{id}", name="manage_app__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('SidereoCoreBundle:App')->find($id); | |
if (!$entity) { | |
throw $this->createNotFoundException('Unable to find App entity.'); | |
} | |
$em->remove($entity); | |
$em->flush(); | |
} | |
return $this->redirect($this->generateUrl('manage_app_')); | |
} | |
/** | |
* Creates a form to delete a App 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('manage_app__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