Skip to content

Instantly share code, notes, and snippets.

@frastel
Last active December 10, 2015 00:49
Show Gist options
  • Save frastel/4354227 to your computer and use it in GitHub Desktop.
Save frastel/4354227 to your computer and use it in GitHub Desktop.
<?php
namespace foo\BarBundle\Controller;
// use statements ...
class FatController extends Controller
{
/**
* @Route("/", name="some_index_route")
* @Template()
*/
public function indexAction($limit)
{
$collection = $this->getDoctrine()->getRepository('Some:Model')->findFooOrderedByBar($limit);
$somethingRelated = array();
foreach ($collection as $model) {
$somethingRelated[$model->getId()] = $this->doSomething($model);
}
return array('collection' => $collection, 'somethingRelated' => $somethingRelated);
}
/**
* @Route("/edit/{id}", name="some_edit_route")
* @Template()
*/
public function updateAction(Request $request, $id)
{
$model = $this->loadModel($id);
$form = $this->createForm('some_type', $model);
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($model);
$em->flush();
$message = \Swift_Message::newInstance()
->setSubject('Foo Bar!')
->setFrom('send@example.com')
->setTo('recipient@example.com')
->setBody('Some content');
$this->get('mailer')->send($message);
$this->get('event_dispatcher')->dispatch('some.update_event', new SomeUpdateEvent($model));
$this->redirect($this->generateUrl('some_redirect_route', array('id' => $model->getId())));
}
}
return array('model' => $model);
}
/**
* @Route("/{id}", name="some_show_route", requirements={"id" = "\d+"})
* @Template()
*/
public function showAction($id)
{
$model = $this->loadModel($id);
return array('model' => $model);
}
private function loadModel($id)
{
$model = $this->getDoctrine()->getRepository('Some:Model')->find($id);
if (!$model) {
throw $this->createNotFoundException(sprintf('omg this model could not be loaded: %s', $id));
}
return $model;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment