Cut down on controller boilerplate

This commit is contained in:
Timothy Warren 2018-07-16 13:50:07 -04:00
parent 828e644fc1
commit 6dcfc2e577
6 changed files with 54 additions and 235 deletions

View File

@ -15,6 +15,11 @@ use Symfony\Component\HttpFoundation\Request;
*/ */
class FlashController extends Controller class FlashController extends Controller
{ {
use FormControllerTrait;
protected const ENTITY = Flash::class;
protected const FORM = FlashType::class;
/** /**
* Lists all flash entities. * Lists all flash entities.
* *
@ -22,17 +27,7 @@ class FlashController extends Controller
*/ */
public function indexAction() public function indexAction()
{ {
$em = $this->getDoctrine()->getManager(); return $this->itemListView('flash/index.html.twig', 'flashes');
$flashes = $em->getRepository(Flash::class)->findBy([], [
'received' => 'DESC',
'brand' => 'ASC',
'model' => 'ASC'
]);
return $this->render('flash/index.html.twig', array(
'flashes' => $flashes,
));
} }
/** /**
@ -42,22 +37,7 @@ class FlashController extends Controller
*/ */
public function newAction(Request $request) public function newAction(Request $request)
{ {
$flash = new Flash(); return $this->itemCreate($request, 'flash/new.html.twig', 'flash', 'flash_show');
$form = $this->createForm(FlashType::class, $flash);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($flash);
$em->flush();
return $this->redirectToRoute('flash_show', array('id' => $flash->getId()));
}
return $this->render('flash/new.html.twig', array(
'flash' => $flash,
'form' => $form->createView(),
));
} }
/** /**
@ -67,12 +47,7 @@ class FlashController extends Controller
*/ */
public function showAction(Flash $flash) public function showAction(Flash $flash)
{ {
$deleteForm = $this->createDeleteForm($flash); return $this->itemView($flash, 'flash/show.html.twig', 'flash');
return $this->render('flash/show.html.twig', array(
'flash' => $flash,
'delete_form' => $deleteForm->createView(),
));
} }
/** /**
@ -82,21 +57,7 @@ class FlashController extends Controller
*/ */
public function editAction(Request $request, Flash $flash) public function editAction(Request $request, Flash $flash)
{ {
$deleteForm = $this->createDeleteForm($flash); return $this->itemUpdate($request, $flash, 'flash/edit.html.twig', 'flash', 'flash_show');
$editForm = $this->createForm(FlashType::class, $flash);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('flash_show', array('id' => $flash->getId()));
}
return $this->render('flash/edit.html.twig', array(
'flash' => $flash,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
} }
/** /**
@ -106,16 +67,7 @@ class FlashController extends Controller
*/ */
public function deleteAction(Request $request, Flash $flash) public function deleteAction(Request $request, Flash $flash)
{ {
$form = $this->createDeleteForm($flash); return $this->itemDelete($request, $flash, 'flash_index');
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove($flash);
$em->flush();
}
return $this->redirectToRoute('flash_index');
} }
/** /**
@ -127,9 +79,6 @@ class FlashController extends Controller
*/ */
private function createDeleteForm(Flash $flash) private function createDeleteForm(Flash $flash)
{ {
return $this->createFormBuilder() return $this->buildForm($flash, 'flash_delete', 'DELETE');
->setAction($this->generateUrl('flash_delete', array('id' => $flash->getId())))
->setMethod('DELETE')
->getForm();
} }
} }

View File

@ -83,7 +83,6 @@ trait FormControllerTrait {
*/ */
protected function itemUpdate(Request $request, $item, string $template, string $templateKey, string $redirectRoute) protected function itemUpdate(Request $request, $item, string $template, string $templateKey, string $redirectRoute)
{ {
$deleteForm = $this->createDeleteForm($item);
$editForm = $this->createForm(self::FORM, $item); $editForm = $this->createForm(self::FORM, $item);
$editForm->handleRequest($request); $editForm->handleRequest($request);
@ -101,9 +100,14 @@ trait FormControllerTrait {
$templateData = [ $templateData = [
$templateKey => $item, $templateKey => $item,
'edit_form' => $editForm->createView(), 'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
]; ];
if (method_exists($this, 'createDeleteForm'))
{
$deleteForm = $this->createDeleteForm($item);
$templateData['delete_form'] = $deleteForm->createView();
}
if (method_exists($this, 'createDeacquireForm')) if (method_exists($this, 'createDeacquireForm'))
{ {
$deacquireForm = $this->createDeacquireForm($item); $deacquireForm = $this->createDeacquireForm($item);

View File

@ -16,6 +16,11 @@ use Symfony\Component\HttpFoundation\{Request, RedirectResponse};
*/ */
class LensesController extends Controller class LensesController extends Controller
{ {
use FormControllerTrait;
protected const ENTITY = Lenses::class;
protected const FORM = LensesType::class;
/** /**
* Lists all lens entities. * Lists all lens entities.
* *
@ -23,9 +28,7 @@ class LensesController extends Controller
*/ */
public function indexAction() public function indexAction()
{ {
$em = $this->getDoctrine()->getManager(); return $this->itemListView('lenses/index.html.twig', 'lenses', [
$lenses = $em->getRepository(Lenses::class)->findBy([], [
'received' => 'DESC', 'received' => 'DESC',
'brand' => 'ASC', 'brand' => 'ASC',
'productLine' => 'ASC', 'productLine' => 'ASC',
@ -33,10 +36,6 @@ class LensesController extends Controller
'minFocalLength' => 'ASC', 'minFocalLength' => 'ASC',
'maxFStop' => 'ASC', 'maxFStop' => 'ASC',
]); ]);
return $this->render('lenses/index.html.twig', array(
'lenses' => $lenses,
));
} }
/** /**
@ -46,22 +45,7 @@ class LensesController extends Controller
*/ */
public function newAction(Request $request) public function newAction(Request $request)
{ {
$lens = new Lenses(); return $this->itemCreate($request, 'lenses/new.html.twig', 'lense', 'lens_show');
$form = $this->createForm(LensesType::class, $lens);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($lens);
$em->flush();
return $this->redirectToRoute('lens_show', array('id' => $lens->getId()));
}
return $this->render('lenses/new.html.twig', array(
'lense' => $lens,
'form' => $form->createView(),
));
} }
/** /**
@ -69,14 +53,9 @@ class LensesController extends Controller
* *
* @Route("/{id}", name="lens_show", methods={"GET"}) * @Route("/{id}", name="lens_show", methods={"GET"})
*/ */
public function showAction(Lenses $lense) public function showAction(Lenses $lens)
{ {
$deleteForm = $this->createDeleteForm($lense); return $this->itemView($lens, 'lenses/show.html.twig', 'lense');
return $this->render('lenses/show.html.twig', array(
'lense' => $lense,
'delete_form' => $deleteForm->createView(),
));
} }
/** /**
@ -86,23 +65,7 @@ class LensesController extends Controller
*/ */
public function editAction(Request $request, Lenses $lens) public function editAction(Request $request, Lenses $lens)
{ {
$deleteForm = $this->createDeleteForm($lens); return $this->itemUpdate($request, $lens, 'lenses/edit.html.twig', 'lense', 'lens_show');
$deacquireForm = $this->createDeacquireForm($lens);
$editForm = $this->createForm(LensesType::class, $lens);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('lens_edit', array('id' => $lens->getId()));
}
return $this->render('lenses/edit.html.twig', array(
'lense' => $lens,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
'deacquire_form' => $deacquireForm->createView()
));
} }
/** /**
@ -115,13 +78,7 @@ class LensesController extends Controller
*/ */
public function deacquireAction(Request $request, Lenses $lens) public function deacquireAction(Request $request, Lenses $lens)
{ {
$form = $this->createDeacquireForm($lens); return $this->itemDeacquire($request, $lens, 'previously-owned-lens_index');
$form->handleRequest($request);
$repository = $this->getDoctrine()->getRepository(Lenses::class);
$repository->deacquire($lens);
return $this->redirectToRoute('lens_index');
} }
/** /**
@ -131,14 +88,7 @@ class LensesController extends Controller
*/ */
public function deleteAction(Request $request, Lenses $lens) public function deleteAction(Request $request, Lenses $lens)
{ {
$form = $this->createDeleteForm($lens); return $this->itemDelete($request, $lens, 'lens_index');
$form->handleRequest($request);
$em = $this->getDoctrine()->getManager();
$em->remove($lens);
$em->flush();
return $this->redirectToRoute('lens_index');
} }
/** /**
@ -150,12 +100,7 @@ class LensesController extends Controller
*/ */
private function createDeleteForm(Lenses $lens): FormInterface private function createDeleteForm(Lenses $lens): FormInterface
{ {
return $this->createFormBuilder() return $this->buildForm($lens, 'lens_delete', 'DELETE');
->setAction(
$this->generateUrl('lens_delete', ['id' => $lens->getId()])
)
->setMethod('DELETE')
->getForm();
} }
@ -168,9 +113,6 @@ class LensesController extends Controller
*/ */
private function createDeacquireForm(Lenses $lens): FormInterface private function createDeacquireForm(Lenses $lens): FormInterface
{ {
return $this->createFormBuilder() return $this->buildForm($lens, 'lens_deacquire');
->setAction($this->generateUrl('lens_deacquire', ['id' => $lens->getId()]))
->setMethod('POST')
->getForm();
} }
} }

View File

@ -43,9 +43,7 @@ class PreviouslyOwnedCameraController extends Controller
*/ */
public function showAction(PreviouslyOwnedCamera $previouslyOwnedCamera) public function showAction(PreviouslyOwnedCamera $previouslyOwnedCamera)
{ {
return $this->render('previouslyownedcamera/show.html.twig', array( return $this->itemView($previouslyOwnedCamera, 'previouslyownedcamera/show.html.twig', 'previouslyOwnedCamera');
'previouslyOwnedCamera' => $previouslyOwnedCamera,
));
} }
/** /**
@ -56,22 +54,7 @@ class PreviouslyOwnedCameraController extends Controller
*/ */
public function editAction(Request $request, PreviouslyOwnedCamera $previouslyOwnedCamera) public function editAction(Request $request, PreviouslyOwnedCamera $previouslyOwnedCamera)
{ {
$editForm = $this->createForm(PreviouslyOwnedCameraType::class, $previouslyOwnedCamera); return $this->itemUpdate($request, $previouslyOwnedCamera, 'previouslyownedcamera/edit.html.twig', 'previouslyOwnedCamera', 'previously-owned-camera_show');
$editForm->handleRequest($request);
$reacquireForm = $this->createReacquireForm($previouslyOwnedCamera);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('previously-owned-camera_edit', array('id' => $previouslyOwnedCamera->getId()));
}
return $this->render('previouslyownedcamera/edit.html.twig', array(
'previouslyOwnedCamera' => $previouslyOwnedCamera,
'edit_form' => $editForm->createView(),
'reacquire_form' => $reacquireForm->createView()
));
} }
/** /**
@ -86,13 +69,7 @@ class PreviouslyOwnedCameraController extends Controller
*/ */
public function reacquireAction(Request $request, PreviouslyOwnedCamera $camera): RedirectResponse public function reacquireAction(Request $request, PreviouslyOwnedCamera $camera): RedirectResponse
{ {
$form = $this->createReacquireForm($camera); return $this->itemReacquire($request, $camera, 'camera_index');
$form->handleRequest($request);
$repository = $this->getDoctrine()->getRepository(PreviouslyOwnedCamera::class);
$repository->reacquire($camera);
return $this->redirectToRoute('camera_index');
} }
@ -105,9 +82,6 @@ class PreviouslyOwnedCameraController extends Controller
*/ */
private function createReacquireForm(PreviouslyOwnedCamera $camera): FormInterface private function createReacquireForm(PreviouslyOwnedCamera $camera): FormInterface
{ {
return $this->createFormBuilder() return $this->buildForm($camera, 'previously-owned-camera_reacquire', 'POST');
->setAction($this->generateUrl('previously-owned-camera_reacquire', ['id' => $camera->getId()]))
->setMethod('POST')
->getForm();
} }
} }

View File

@ -15,6 +15,10 @@ use Symfony\Component\HttpFoundation\Request;
*/ */
class PreviouslyOwnedFlashController extends Controller class PreviouslyOwnedFlashController extends Controller
{ {
use FormControllerTrait;
protected const ENTITY = PreviouslyOwnedFlash::class;
protected const FORM = PreviouslyOwnedFlashType::class;
/** /**
* Lists all previouslyOwnedFlash entities. * Lists all previouslyOwnedFlash entities.
* *
@ -22,16 +26,7 @@ class PreviouslyOwnedFlashController extends Controller
*/ */
public function indexAction() public function indexAction()
{ {
$em = $this->getDoctrine()->getManager(); return $this->itemListView('previouslyownedflash/index.html.twig', 'previouslyOwnedFlashes');
$previouslyOwnedFlashes = $em->getRepository(PreviouslyOwnedFlash::class)->findBy([], [
'brand' => 'ASC',
'model' => 'ASC'
]);
return $this->render('previouslyownedflash/index.html.twig', array(
'previouslyOwnedFlashes' => $previouslyOwnedFlashes,
));
} }
/** /**
@ -41,22 +36,7 @@ class PreviouslyOwnedFlashController extends Controller
*/ */
public function newAction(Request $request) public function newAction(Request $request)
{ {
$previouslyOwnedFlash = new Previouslyownedflash(); return $this->itemCreate($request, 'previouslyownedflash/new.html.twig', 'previouslyOwnedFlash', 'previously-owned-flash_show');
$form = $this->createForm(PreviouslyOwnedFlashType::class, $previouslyOwnedFlash);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($previouslyOwnedFlash);
$em->flush();
return $this->redirectToRoute('previously-owned-flash_show', array('id' => $previouslyOwnedFlash->getId()));
}
return $this->render('previouslyownedflash/new.html.twig', array(
'previouslyOwnedFlash' => $previouslyOwnedFlash,
'form' => $form->createView(),
));
} }
/** /**
@ -66,9 +46,7 @@ class PreviouslyOwnedFlashController extends Controller
*/ */
public function showAction(PreviouslyOwnedFlash $previouslyOwnedFlash) public function showAction(PreviouslyOwnedFlash $previouslyOwnedFlash)
{ {
return $this->render('previouslyownedflash/show.html.twig', array( return $this->itemView($previouslyOwnedFlash, 'previouslyownedcamera/show.html.twig', 'previouslyOwnedFlash');
'previouslyOwnedFlash' => $previouslyOwnedFlash
));
} }
/** /**
@ -78,18 +56,6 @@ class PreviouslyOwnedFlashController extends Controller
*/ */
public function editAction(Request $request, PreviouslyOwnedFlash $previouslyOwnedFlash) public function editAction(Request $request, PreviouslyOwnedFlash $previouslyOwnedFlash)
{ {
$editForm = $this->createForm(PreviouslyOwnedFlashType::class, $previouslyOwnedFlash); return $this->itemUpdate($request, $previouslyOwnedFlash, 'previouslyownedflash/edit.html.twig', 'previouslyOwnedFlash', 'previously-owned-flash_show');
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('previously-owned-flash_edit', array('id' => $previouslyOwnedFlash->getId()));
}
return $this->render('previouslyownedflash/edit.html.twig', array(
'previouslyOwnedFlash' => $previouslyOwnedFlash,
'edit_form' => $editForm->createView(),
));
} }
} }

View File

@ -3,17 +3,21 @@
namespace App\Controller; namespace App\Controller;
use App\Entity\PreviouslyOwnedLenses; use App\Entity\PreviouslyOwnedLenses;
use App\Form\PreviouslyOwnedLensesType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
/** /**
* Previouslyownedlense controller.
*
* @Route("previously-owned-lens") * @Route("previously-owned-lens")
*/ */
class PreviouslyOwnedLensesController extends Controller class PreviouslyOwnedLensesController extends Controller
{ {
use FormControllerTrait;
protected const ENTITY = PreviouslyOwnedLenses::class;
protected const FORM = PreviouslyOwnedLensesType::class;
/** /**
* Lists all previouslyOwnedLense entities. * Lists all previouslyOwnedLense entities.
* *
@ -21,19 +25,13 @@ class PreviouslyOwnedLensesController extends Controller
*/ */
public function indexAction() public function indexAction()
{ {
$em = $this->getDoctrine()->getManager(); return $this->itemListView('previouslyownedlenses/index.html.twig', 'previouslyOwnedLenses', [
$previouslyOwnedLenses = $em->getRepository(PreviouslyOwnedLenses::class)->findBy([], [
'brand' => 'ASC', 'brand' => 'ASC',
'productLine' => 'ASC', 'productLine' => 'ASC',
'mount' => 'ASC', 'mount' => 'ASC',
'minFocalLength' => 'ASC', 'minFocalLength' => 'ASC',
'maxFStop' => 'ASC', 'maxFStop' => 'ASC',
]); ]);
return $this->render('previouslyownedlenses/index.html.twig', array(
'previouslyOwnedLenses' => $previouslyOwnedLenses,
));
} }
/** /**
@ -41,11 +39,9 @@ class PreviouslyOwnedLensesController extends Controller
* *
* @Route("/{id}", name="previously-owned-lens_show", methods={"GET"}) * @Route("/{id}", name="previously-owned-lens_show", methods={"GET"})
*/ */
public function showAction(PreviouslyOwnedLenses $previouslyOwnedLense) public function showAction(PreviouslyOwnedLenses $previouslyOwnedLens)
{ {
return $this->render('previouslyownedlenses/show.html.twig', array( return $this->itemView($previouslyOwnedLens, 'previouslyownedlenses/show.html.twig', 'previouslyOwnedLense');
'previouslyOwnedLense' => $previouslyOwnedLense,
));
} }
/** /**
@ -53,20 +49,8 @@ class PreviouslyOwnedLensesController extends Controller
* *
* @Route("/{id}/edit", name="previously-owned-lens_edit", methods={"GET", "POST"}) * @Route("/{id}/edit", name="previously-owned-lens_edit", methods={"GET", "POST"})
*/ */
public function editAction(Request $request, PreviouslyOwnedLenses $previouslyOwnedLense) public function editAction(Request $request, PreviouslyOwnedLenses $previouslyOwnedLens)
{ {
$editForm = $this->createForm('App\Form\PreviouslyOwnedLensesType', $previouslyOwnedLense); return $this->itemUpdate($request, $previouslyOwnedLens, 'previouslyownedlenses/edit.html.twig', 'previouslyOwnedLense', 'previously-owned-lens_show');
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('previously-owned-lens_edit', array('id' => $previouslyOwnedLense->getId()));
}
return $this->render('previouslyownedlenses/edit.html.twig', array(
'previouslyOwnedLense' => $previouslyOwnedLense,
'edit_form' => $editForm->createView(),
));
} }
} }