Skip to content

Instantly share code, notes, and snippets.

@j
Created June 17, 2011 23:36
Show Gist options
  • Save j/1032592 to your computer and use it in GitHub Desktop.
Save j/1032592 to your computer and use it in GitHub Desktop.
<?php
namespace JStout\MainBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller,
Sensio\Bundle\FrameworkExtraBundle\Configuration as Extra,
Symfony\Component\HttpKernel\Exception\NotFoundHttpException,
JStout\MainBundle\Entity,
JStout\MainBundle\Form;
/**
* @Extra\Route("/admin")
* @Extra\Template()
*/
class AdminController extends Controller
{
/**
* @Extra\Route(name="admin_index")
* @Extra\Template()
*/
public function indexAction()
{
return array();
}
/**
* @Extra\Route("/advertiser", name="admin_advertiser")
* @Extra\Route("/advertiser/{id}/edit", name="admin_advertiser_edit")
* @Extra\Template()
*/
public function advertiserAction($id = null)
{
$advertiser = $this->_getObject('Advertiser', $id);
$form = $this->get('form.factory')->create(new Form\AdvertiserType(), $advertiser);
$formHandler = $this->get('form.handler')->create(new Form\AdvertiserHandler(), $form);
// process form
if ($formHandler->process()) {
$this->get('session')->setFlash('notice', 'Successfully ' . (!$this->_isEdit($advertiser) ? 'added' : 'edited') . ' advertiser!');
return $this->redirect($this->generateUrl('admin_advertiser'));
}
return array(
'form' => $form->createView(),
'advertisers' => $this->_paginate('Advertiser'),
'new' => !$this->_isEdit($advertiser)
);
}
[.... a lot of other methods go here ....]
/**
* Helper function to allow ability to handle two routes for one controller.
* This creates a new entity instance or returns the requested $id of the Entity
*
* @param $entityName
* @param null $id
* @return Entity Object or NotFoundHttpException
*/
protected function _getObject($entityName, $id = null)
{
// Find object
if (null !== $id) {
if (!$object = $this->get('doctrine')->getEntityManager()->find('JStoutMainBundle:' . $entityName, $id)) {
throw new NotFoundHttpException('The page you requested does not exist!');
}
return $object;
}
// Initialize new object
$entityName = 'JStout\MainBundle\Entity\\' . $entityName;
if (class_exists($entityName)) {
return new $entityName();
}
throw new NotFoundHttpException($entityName . ' was not able to be created.');
}
}
default:
resource: "@JStoutMainBundle/Controller/DefaultController.php"
type: annotation
admin:
resource: "@JStoutMainBundle/Controller/AdminController.php"
type: annotation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment