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('/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, WishRepository $wishRepository): Response { return $this->render('app/offer.html.twig', [ 'offer' => $offering, 'wishes' => $wishRepository->findByUser($offering->getByUser()), ]); } }