split controllers
This commit is contained in:
parent
35254a0bb4
commit
d69842ba07
5 changed files with 151 additions and 64 deletions
|
@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
71
src/Controller/OfferController.php
Normal file
71
src/Controller/OfferController.php
Normal file
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Offering;
|
||||
use App\Form\OfferingFormType;
|
||||
|
||||
use App\Repository\OfferingRepository;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Twig\Environment;
|
||||
|
||||
class OfferController extends AbstractController
|
||||
{
|
||||
#[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): Response
|
||||
{
|
||||
return $this->render('app/offer.html.twig', [
|
||||
'offer' => $offering,
|
||||
]);
|
||||
}
|
||||
}
|
18
src/Controller/UserController.php
Normal file
18
src/Controller/UserController.php
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
class UserController extends AbstractController
|
||||
{
|
||||
#[Route('/user', name: 'user_page')]
|
||||
public function user(): Response
|
||||
{
|
||||
return $this->render('user/index.html.twig', [
|
||||
'user' => $this->getUser(),
|
||||
]);
|
||||
}
|
||||
}
|
Reference in a new issue