From d69842ba07a072894ebc42905e793d3bb5121da7 Mon Sep 17 00:00:00 2001 From: thisfro Date: Mon, 3 May 2021 18:32:31 +0200 Subject: [PATCH] split controllers --- src/Controller/AppController.php | 64 --------------------------- src/Controller/OfferController.php | 71 ++++++++++++++++++++++++++++++ src/Controller/UserController.php | 18 ++++++++ templates/offer/index.html.twig | 20 +++++++++ templates/user/index.html.twig | 42 ++++++++++++++++++ 5 files changed, 151 insertions(+), 64 deletions(-) create mode 100644 src/Controller/OfferController.php create mode 100644 src/Controller/UserController.php create mode 100644 templates/offer/index.html.twig create mode 100644 templates/user/index.html.twig diff --git a/src/Controller/AppController.php b/src/Controller/AppController.php index 279284f..4b85e62 100644 --- a/src/Controller/AppController.php +++ b/src/Controller/AppController.php @@ -2,8 +2,6 @@ namespace App\Controller; -use App\Entity\Offering; -use App\Form\OfferingFormType; use App\Repository\OfferingRepository; use Doctrine\ORM\EntityManagerInterface; @@ -21,66 +19,4 @@ class AppController extends AbstractController { $this->entityManager = $entityManager; } - - #[Route('/', name: 'homepage')] - public function index(Environment $twig, OfferingRepository $offeringRepository): Response - { - return new Response($twig->render('app/index.html.twig', [ - 'offerings' => $offeringRepository->findAll(), - ])); - } - - #[Route('/user', name: 'user_page')] - public function user(): Response - { - return $this->render('app/user.html.twig', [ - 'user' => $this->getUser(), - ]); - } - - #[Route('/new', name: 'new_listing')] - public function new_listing(Request $request, string $photoDir): Response - { - $offering = new Offering(); - $form = $this->createForm(OfferingFormType::class, $offering); - $user = $this->getUser(); - - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - $offering->setByUser($user); - $offering->setCreatedAt(new \DateTime()); - - if ($photo = $form['photo']->getData()) { - $filename = bin2hex(random_bytes(6)).'.'.$photo->guessExtension(); - try { - $photo->move($photoDir, $filename); - } catch (FileException $e) { - // unable to upload the photo, give up - $this->addFlash("error", "There was an error uploading the photo: ".$e); - return $this->redirectToRoute('new_listing'); - } - $offering->setPhotoFilename($filename); - } - - $this->entityManager->persist($offering); - $this->entityManager->flush(); - - $this->addFlash("success", "Successfully added the new offering!"); - return $this->redirectToRoute('homepage'); - } - - return $this->render('app/new_listing.html.twig', [ - 'user' => $this->getUser(), - 'offering_form' => $form->createView(), - ]); - } - - #[Route('/offer/{id}', name: 'show_offer')] - public function show_offer(Offering $offering): Response - { - return $this->render('app/offer.html.twig', [ - 'offer' => $offering, - ]); - } } diff --git a/src/Controller/OfferController.php b/src/Controller/OfferController.php new file mode 100644 index 0000000..a3ad958 --- /dev/null +++ b/src/Controller/OfferController.php @@ -0,0 +1,71 @@ +render('app/index.html.twig', [ + 'offerings' => $offeringRepository->findAll(), + ])); + } + + #[Route('/new', name: 'new_listing')] + public function new_listing(Request $request, string $photoDir): Response + { + $offering = new Offering(); + $form = $this->createForm(OfferingFormType::class, $offering); + $user = $this->getUser(); + + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $offering->setByUser($user); + $offering->setCreatedAt(new \DateTime()); + + if ($photo = $form['photo']->getData()) { + $filename = bin2hex(random_bytes(6)).'.'.$photo->guessExtension(); + try { + $photo->move($photoDir, $filename); + } catch (FileException $e) { + // unable to upload the photo, give up + $this->addFlash("error", "There was an error uploading the photo: ".$e); + return $this->redirectToRoute('new_listing'); + } + $offering->setPhotoFilename($filename); + } + + $this->entityManager->persist($offering); + $this->entityManager->flush(); + + $this->addFlash("success", "Successfully added the new offering!"); + return $this->redirectToRoute('homepage'); + } + + return $this->render('app/new_listing.html.twig', [ + 'user' => $this->getUser(), + 'offering_form' => $form->createView(), + ]); + } + + #[Route('/offer/{id}', name: 'show_offer')] + public function show_offer(Offering $offering): Response + { + return $this->render('app/offer.html.twig', [ + 'offer' => $offering, + ]); + } +} diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php new file mode 100644 index 0000000..00869f5 --- /dev/null +++ b/src/Controller/UserController.php @@ -0,0 +1,18 @@ +render('user/index.html.twig', [ + 'user' => $this->getUser(), + ]); + } +} diff --git a/templates/offer/index.html.twig b/templates/offer/index.html.twig new file mode 100644 index 0000000..9cce346 --- /dev/null +++ b/templates/offer/index.html.twig @@ -0,0 +1,20 @@ +{% extends 'base.html.twig' %} + +{% block title %}Hello OfferController!{% endblock %} + +{% block body %} + + +
+

Hello {{ controller_name }}! ✅

+ + This friendly message is coming from: + +
+{% endblock %} diff --git a/templates/user/index.html.twig b/templates/user/index.html.twig new file mode 100644 index 0000000..d11cfa9 --- /dev/null +++ b/templates/user/index.html.twig @@ -0,0 +1,42 @@ +{% extends 'base.html.twig' %} + +{% block title %}User{% endblock %} + +{% block body %} + {% for message in app.flashes('success') %} + + {% endfor %} + +
+

Hello {{ user.username }}!

+

+ +
+
+

Change your user data

+
+ + +
+
+ + +
+ + + + +
+
+ +
+

Delete Account

+ +
+{% endblock %}