Skip to content

Instantly share code, notes, and snippets.

@cyb3rd4d
Last active December 31, 2015 11:09
Show Gist options
  • Save cyb3rd4d/7978315 to your computer and use it in GitHub Desktop.
Save cyb3rd4d/7978315 to your computer and use it in GitHub Desktop.
A complete example of a CRUD manager implementation in a controller.
<?php
namespace Martial\LabBundle\Controller;
use Martial\CrudBundle\Exception\CrudException;
use Martial\LabBundle\Entity\Article;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller
{
/**
* Get the article list
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function indexAction()
{
$articles = $this->getCrudManager()->getRepository()->findAll();
return $this->render('MartialLabBundle:Default:index.html.twig', array('articles' => $articles));
}
/**
* Show an article
*
* @param int $id
* @return \Symfony\Component\HttpFoundation\Response
*/
public function showAction($id)
{
$article = $this->findArticle($id);
return $this->render('MartialLabBundle:Default:show.html.twig', array('article' => $article));
}
/**
* Display a form to create an article
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function newAction()
{
$form = $this->getCrudManager()->createForm();
return $this->render('MartialLabBundle:Default:new.html.twig', array('form' => $form->createView()));
}
/**
* Save a new article
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function createAction()
{
return $this->saveArticle();
}
/**
* Edit an existing article
*
* @param int $id
* @return \Symfony\Component\HttpFoundation\Response
*/
public function editAction($id)
{
$article = $this->findArticle($id);
$form = $this->getCrudManager()->createForm($article);
return $this->render('MartialLabBundle:Default:edit.html.twig', array(
'form' => $form->createView(),
'article' => $article
)
);
}
/**
* Update an article
*
* @param int $id
* @return \Symfony\Component\HttpFoundation\Response
*/
public function updateAction($id)
{
$article = $this->findArticle($id);
return $this->saveArticle($article);
}
/**
* Delete an article
*
* @param int $id
* @return \Symfony\Component\HttpFoundation\Response
*/
public function deleteAction($id)
{
$article = $this->findArticle($id);
$this->getCrudManager()->delete($article);
return $this->redirect($this->generateUrl('martial_lab_homepage'));
}
/**
* Find an article
*
* @param int $id
* @return object
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
private function findArticle($id)
{
$article = $this->getCrudManager()->find($id);
if (!$article) {
throw $this->createNotFoundException('Article not found');
}
return $article;
}
/**
* Return the CRUD manager
*
* @return \Martial\CrudBundle\Manager\CrudManagerInterface
*/
private function getCrudManager()
{
return $this->get('martial.crud')->getCrudManager('article');
}
/**
* Save an article
*
* @param Article $article
* @return \Symfony\Component\HttpFoundation\Response
*/
private function saveArticle($article = null)
{
try {
$this->getCrudManager()->saveForm($article);
return $this->redirect($this->generateUrl('martial_lab_show', array(
'id' => $article->getId()
)
)
);
} catch (CrudException $e) {
return $this->render('MartialLabBundle:Default:edit.html.twig', array(
'form' => $this->getCrudManager()->getCurrentForm()->createView(),
'article' => $this->getCrudManager()->getCurrentEntity()
)
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment