Last active
January 1, 2016 21:59
-
-
Save cyb3rd4d/8206790 to your computer and use it in GitHub Desktop.
A complete example of a CRUD custom service implementation in a controller.
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 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->getService()->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->getService()->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->getService()->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->getService()->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->getService()->find($id); | |
if (!$article) { | |
throw $this->createNotFoundException('Article not found'); | |
} | |
return $article; | |
} | |
/** | |
* Save an article | |
* | |
* @param Article $article | |
* @return \Symfony\Component\HttpFoundation\Response | |
*/ | |
private function saveArticle($article = null) | |
{ | |
try { | |
$this->getService()->saveForm($article); | |
return $this->render('MartialLabBundle:Default:show.html.twig', array( | |
'article' => $this->getService()->getCurrentEntity() | |
) | |
); | |
} catch (CrudException $e) { | |
return $this->render('MartialLabBundle:Default:new.html.twig', array( | |
'form' => $this->getService()->getCurrentForm()->createView() | |
) | |
); | |
} | |
} | |
/** | |
* @return \Martial\LabBundle\Service\ArticleService | |
*/ | |
private function getService() | |
{ | |
return $this->get('martial_lab.article'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment