Cut down on controller boilerplate
This commit is contained in:
parent
828e644fc1
commit
6dcfc2e577
@ -15,6 +15,11 @@ use Symfony\Component\HttpFoundation\Request;
|
||||
*/
|
||||
class FlashController extends Controller
|
||||
{
|
||||
use FormControllerTrait;
|
||||
|
||||
protected const ENTITY = Flash::class;
|
||||
protected const FORM = FlashType::class;
|
||||
|
||||
/**
|
||||
* Lists all flash entities.
|
||||
*
|
||||
@ -22,17 +27,7 @@ class FlashController extends Controller
|
||||
*/
|
||||
public function indexAction()
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$flashes = $em->getRepository(Flash::class)->findBy([], [
|
||||
'received' => 'DESC',
|
||||
'brand' => 'ASC',
|
||||
'model' => 'ASC'
|
||||
]);
|
||||
|
||||
return $this->render('flash/index.html.twig', array(
|
||||
'flashes' => $flashes,
|
||||
));
|
||||
return $this->itemListView('flash/index.html.twig', 'flashes');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -42,22 +37,7 @@ class FlashController extends Controller
|
||||
*/
|
||||
public function newAction(Request $request)
|
||||
{
|
||||
$flash = new Flash();
|
||||
$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(),
|
||||
));
|
||||
return $this->itemCreate($request, 'flash/new.html.twig', 'flash', 'flash_show');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -67,12 +47,7 @@ class FlashController extends Controller
|
||||
*/
|
||||
public function showAction(Flash $flash)
|
||||
{
|
||||
$deleteForm = $this->createDeleteForm($flash);
|
||||
|
||||
return $this->render('flash/show.html.twig', array(
|
||||
'flash' => $flash,
|
||||
'delete_form' => $deleteForm->createView(),
|
||||
));
|
||||
return $this->itemView($flash, 'flash/show.html.twig', 'flash');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -82,21 +57,7 @@ class FlashController extends Controller
|
||||
*/
|
||||
public function editAction(Request $request, Flash $flash)
|
||||
{
|
||||
$deleteForm = $this->createDeleteForm($flash);
|
||||
$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(),
|
||||
));
|
||||
return $this->itemUpdate($request, $flash, 'flash/edit.html.twig', 'flash', 'flash_show');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -106,16 +67,7 @@ class FlashController extends Controller
|
||||
*/
|
||||
public function deleteAction(Request $request, Flash $flash)
|
||||
{
|
||||
$form = $this->createDeleteForm($flash);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$em->remove($flash);
|
||||
$em->flush();
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('flash_index');
|
||||
return $this->itemDelete($request, $flash, 'flash_index');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -127,9 +79,6 @@ class FlashController extends Controller
|
||||
*/
|
||||
private function createDeleteForm(Flash $flash)
|
||||
{
|
||||
return $this->createFormBuilder()
|
||||
->setAction($this->generateUrl('flash_delete', array('id' => $flash->getId())))
|
||||
->setMethod('DELETE')
|
||||
->getForm();
|
||||
return $this->buildForm($flash, 'flash_delete', 'DELETE');
|
||||
}
|
||||
}
|
||||
|
@ -83,7 +83,6 @@ trait FormControllerTrait {
|
||||
*/
|
||||
protected function itemUpdate(Request $request, $item, string $template, string $templateKey, string $redirectRoute)
|
||||
{
|
||||
$deleteForm = $this->createDeleteForm($item);
|
||||
$editForm = $this->createForm(self::FORM, $item);
|
||||
$editForm->handleRequest($request);
|
||||
|
||||
@ -101,9 +100,14 @@ trait FormControllerTrait {
|
||||
$templateData = [
|
||||
$templateKey => $item,
|
||||
'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'))
|
||||
{
|
||||
$deacquireForm = $this->createDeacquireForm($item);
|
||||
|
@ -16,6 +16,11 @@ use Symfony\Component\HttpFoundation\{Request, RedirectResponse};
|
||||
*/
|
||||
class LensesController extends Controller
|
||||
{
|
||||
use FormControllerTrait;
|
||||
|
||||
protected const ENTITY = Lenses::class;
|
||||
protected const FORM = LensesType::class;
|
||||
|
||||
/**
|
||||
* Lists all lens entities.
|
||||
*
|
||||
@ -23,9 +28,7 @@ class LensesController extends Controller
|
||||
*/
|
||||
public function indexAction()
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$lenses = $em->getRepository(Lenses::class)->findBy([], [
|
||||
return $this->itemListView('lenses/index.html.twig', 'lenses', [
|
||||
'received' => 'DESC',
|
||||
'brand' => 'ASC',
|
||||
'productLine' => 'ASC',
|
||||
@ -33,10 +36,6 @@ class LensesController extends Controller
|
||||
'minFocalLength' => '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)
|
||||
{
|
||||
$lens = new Lenses();
|
||||
$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(),
|
||||
));
|
||||
return $this->itemCreate($request, 'lenses/new.html.twig', 'lense', 'lens_show');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -69,14 +53,9 @@ class LensesController extends Controller
|
||||
*
|
||||
* @Route("/{id}", name="lens_show", methods={"GET"})
|
||||
*/
|
||||
public function showAction(Lenses $lense)
|
||||
public function showAction(Lenses $lens)
|
||||
{
|
||||
$deleteForm = $this->createDeleteForm($lense);
|
||||
|
||||
return $this->render('lenses/show.html.twig', array(
|
||||
'lense' => $lense,
|
||||
'delete_form' => $deleteForm->createView(),
|
||||
));
|
||||
return $this->itemView($lens, 'lenses/show.html.twig', 'lense');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -86,23 +65,7 @@ class LensesController extends Controller
|
||||
*/
|
||||
public function editAction(Request $request, Lenses $lens)
|
||||
{
|
||||
$deleteForm = $this->createDeleteForm($lens);
|
||||
$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()
|
||||
));
|
||||
return $this->itemUpdate($request, $lens, 'lenses/edit.html.twig', 'lense', 'lens_show');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -115,13 +78,7 @@ class LensesController extends Controller
|
||||
*/
|
||||
public function deacquireAction(Request $request, Lenses $lens)
|
||||
{
|
||||
$form = $this->createDeacquireForm($lens);
|
||||
$form->handleRequest($request);
|
||||
|
||||
$repository = $this->getDoctrine()->getRepository(Lenses::class);
|
||||
$repository->deacquire($lens);
|
||||
|
||||
return $this->redirectToRoute('lens_index');
|
||||
return $this->itemDeacquire($request, $lens, 'previously-owned-lens_index');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -131,14 +88,7 @@ class LensesController extends Controller
|
||||
*/
|
||||
public function deleteAction(Request $request, Lenses $lens)
|
||||
{
|
||||
$form = $this->createDeleteForm($lens);
|
||||
$form->handleRequest($request);
|
||||
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$em->remove($lens);
|
||||
$em->flush();
|
||||
|
||||
return $this->redirectToRoute('lens_index');
|
||||
return $this->itemDelete($request, $lens, 'lens_index');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -150,12 +100,7 @@ class LensesController extends Controller
|
||||
*/
|
||||
private function createDeleteForm(Lenses $lens): FormInterface
|
||||
{
|
||||
return $this->createFormBuilder()
|
||||
->setAction(
|
||||
$this->generateUrl('lens_delete', ['id' => $lens->getId()])
|
||||
)
|
||||
->setMethod('DELETE')
|
||||
->getForm();
|
||||
return $this->buildForm($lens, 'lens_delete', 'DELETE');
|
||||
}
|
||||
|
||||
|
||||
@ -168,9 +113,6 @@ class LensesController extends Controller
|
||||
*/
|
||||
private function createDeacquireForm(Lenses $lens): FormInterface
|
||||
{
|
||||
return $this->createFormBuilder()
|
||||
->setAction($this->generateUrl('lens_deacquire', ['id' => $lens->getId()]))
|
||||
->setMethod('POST')
|
||||
->getForm();
|
||||
return $this->buildForm($lens, 'lens_deacquire');
|
||||
}
|
||||
}
|
||||
|
@ -43,9 +43,7 @@ class PreviouslyOwnedCameraController extends Controller
|
||||
*/
|
||||
public function showAction(PreviouslyOwnedCamera $previouslyOwnedCamera)
|
||||
{
|
||||
return $this->render('previouslyownedcamera/show.html.twig', array(
|
||||
'previouslyOwnedCamera' => $previouslyOwnedCamera,
|
||||
));
|
||||
return $this->itemView($previouslyOwnedCamera, 'previouslyownedcamera/show.html.twig', 'previouslyOwnedCamera');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -56,22 +54,7 @@ class PreviouslyOwnedCameraController extends Controller
|
||||
*/
|
||||
public function editAction(Request $request, PreviouslyOwnedCamera $previouslyOwnedCamera)
|
||||
{
|
||||
$editForm = $this->createForm(PreviouslyOwnedCameraType::class, $previouslyOwnedCamera);
|
||||
$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()
|
||||
));
|
||||
return $this->itemUpdate($request, $previouslyOwnedCamera, 'previouslyownedcamera/edit.html.twig', 'previouslyOwnedCamera', 'previously-owned-camera_show');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -86,13 +69,7 @@ class PreviouslyOwnedCameraController extends Controller
|
||||
*/
|
||||
public function reacquireAction(Request $request, PreviouslyOwnedCamera $camera): RedirectResponse
|
||||
{
|
||||
$form = $this->createReacquireForm($camera);
|
||||
$form->handleRequest($request);
|
||||
|
||||
$repository = $this->getDoctrine()->getRepository(PreviouslyOwnedCamera::class);
|
||||
$repository->reacquire($camera);
|
||||
|
||||
return $this->redirectToRoute('camera_index');
|
||||
return $this->itemReacquire($request, $camera, 'camera_index');
|
||||
}
|
||||
|
||||
|
||||
@ -105,9 +82,6 @@ class PreviouslyOwnedCameraController extends Controller
|
||||
*/
|
||||
private function createReacquireForm(PreviouslyOwnedCamera $camera): FormInterface
|
||||
{
|
||||
return $this->createFormBuilder()
|
||||
->setAction($this->generateUrl('previously-owned-camera_reacquire', ['id' => $camera->getId()]))
|
||||
->setMethod('POST')
|
||||
->getForm();
|
||||
return $this->buildForm($camera, 'previously-owned-camera_reacquire', 'POST');
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,10 @@ use Symfony\Component\HttpFoundation\Request;
|
||||
*/
|
||||
class PreviouslyOwnedFlashController extends Controller
|
||||
{
|
||||
use FormControllerTrait;
|
||||
|
||||
protected const ENTITY = PreviouslyOwnedFlash::class;
|
||||
protected const FORM = PreviouslyOwnedFlashType::class;
|
||||
/**
|
||||
* Lists all previouslyOwnedFlash entities.
|
||||
*
|
||||
@ -22,16 +26,7 @@ class PreviouslyOwnedFlashController extends Controller
|
||||
*/
|
||||
public function indexAction()
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$previouslyOwnedFlashes = $em->getRepository(PreviouslyOwnedFlash::class)->findBy([], [
|
||||
'brand' => 'ASC',
|
||||
'model' => 'ASC'
|
||||
]);
|
||||
|
||||
return $this->render('previouslyownedflash/index.html.twig', array(
|
||||
'previouslyOwnedFlashes' => $previouslyOwnedFlashes,
|
||||
));
|
||||
return $this->itemListView('previouslyownedflash/index.html.twig', 'previouslyOwnedFlashes');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -41,22 +36,7 @@ class PreviouslyOwnedFlashController extends Controller
|
||||
*/
|
||||
public function newAction(Request $request)
|
||||
{
|
||||
$previouslyOwnedFlash = new Previouslyownedflash();
|
||||
$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(),
|
||||
));
|
||||
return $this->itemCreate($request, 'previouslyownedflash/new.html.twig', 'previouslyOwnedFlash', 'previously-owned-flash_show');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -66,9 +46,7 @@ class PreviouslyOwnedFlashController extends Controller
|
||||
*/
|
||||
public function showAction(PreviouslyOwnedFlash $previouslyOwnedFlash)
|
||||
{
|
||||
return $this->render('previouslyownedflash/show.html.twig', array(
|
||||
'previouslyOwnedFlash' => $previouslyOwnedFlash
|
||||
));
|
||||
return $this->itemView($previouslyOwnedFlash, 'previouslyownedcamera/show.html.twig', 'previouslyOwnedFlash');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -78,18 +56,6 @@ class PreviouslyOwnedFlashController extends Controller
|
||||
*/
|
||||
public function editAction(Request $request, PreviouslyOwnedFlash $previouslyOwnedFlash)
|
||||
{
|
||||
$editForm = $this->createForm(PreviouslyOwnedFlashType::class, $previouslyOwnedFlash);
|
||||
$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(),
|
||||
));
|
||||
return $this->itemUpdate($request, $previouslyOwnedFlash, 'previouslyownedflash/edit.html.twig', 'previouslyOwnedFlash', 'previously-owned-flash_show');
|
||||
}
|
||||
}
|
||||
|
@ -3,17 +3,21 @@
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\PreviouslyOwnedLenses;
|
||||
use App\Form\PreviouslyOwnedLensesType;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Previouslyownedlense controller.
|
||||
*
|
||||
* @Route("previously-owned-lens")
|
||||
*/
|
||||
class PreviouslyOwnedLensesController extends Controller
|
||||
{
|
||||
use FormControllerTrait;
|
||||
|
||||
protected const ENTITY = PreviouslyOwnedLenses::class;
|
||||
protected const FORM = PreviouslyOwnedLensesType::class;
|
||||
|
||||
/**
|
||||
* Lists all previouslyOwnedLense entities.
|
||||
*
|
||||
@ -21,19 +25,13 @@ class PreviouslyOwnedLensesController extends Controller
|
||||
*/
|
||||
public function indexAction()
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$previouslyOwnedLenses = $em->getRepository(PreviouslyOwnedLenses::class)->findBy([], [
|
||||
return $this->itemListView('previouslyownedlenses/index.html.twig', 'previouslyOwnedLenses', [
|
||||
'brand' => 'ASC',
|
||||
'productLine' => 'ASC',
|
||||
'mount' => 'ASC',
|
||||
'minFocalLength' => '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"})
|
||||
*/
|
||||
public function showAction(PreviouslyOwnedLenses $previouslyOwnedLense)
|
||||
public function showAction(PreviouslyOwnedLenses $previouslyOwnedLens)
|
||||
{
|
||||
return $this->render('previouslyownedlenses/show.html.twig', array(
|
||||
'previouslyOwnedLense' => $previouslyOwnedLense,
|
||||
));
|
||||
return $this->itemView($previouslyOwnedLens, 'previouslyownedlenses/show.html.twig', 'previouslyOwnedLense');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -53,20 +49,8 @@ class PreviouslyOwnedLensesController extends Controller
|
||||
*
|
||||
* @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);
|
||||
$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(),
|
||||
));
|
||||
return $this->itemUpdate($request, $previouslyOwnedLens, 'previouslyownedlenses/edit.html.twig', 'previouslyOwnedLense', 'previously-owned-lens_show');
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user