split controllers

This commit is contained in:
Jannis Portmann 2021-05-03 18:32:31 +02:00
parent 35254a0bb4
commit d69842ba07
5 changed files with 151 additions and 64 deletions

View file

@ -2,8 +2,6 @@
namespace App\Controller; namespace App\Controller;
use App\Entity\Offering;
use App\Form\OfferingFormType;
use App\Repository\OfferingRepository; use App\Repository\OfferingRepository;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
@ -21,66 +19,4 @@ class AppController extends AbstractController
{ {
$this->entityManager = $entityManager; $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,
]);
}
} }

View 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,
]);
}
}

View 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(),
]);
}
}

View file

@ -0,0 +1,20 @@
{% extends 'base.html.twig' %}
{% block title %}Hello OfferController!{% endblock %}
{% block body %}
<style>
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
</style>
<div class="example-wrapper">
<h1>Hello {{ controller_name }}! ✅</h1>
This friendly message is coming from:
<ul>
<li>Your controller at <code><a href="{{ '/home/thisfro/repos/plant-exchange/src/Controller/OfferController.php'|file_link(0) }}">src/Controller/OfferController.php</a></code></li>
<li>Your template at <code><a href="{{ '/home/thisfro/repos/plant-exchange/templates/offer/index.html.twig'|file_link(0) }}">templates/offer/index.html.twig</a></code></li>
</ul>
</div>
{% endblock %}

View file

@ -0,0 +1,42 @@
{% extends 'base.html.twig' %}
{% block title %}User{% endblock %}
{% block body %}
{% for message in app.flashes('success') %}
<div class="alert alert-success" role="alert">
{{ message }}
</div>
{% endfor %}
<div class="mb-3">
<h1>Hello {{ user.username }}!</p>
</div>
<div class="mb-3">
<form method="post">
<h3 class="mb-3 font-weight-normal">Change your user data</h3>
<div class="mb-3">
<label for="inputEmail" class="form-label">Email address</label>
<input name="email" type="email" class="form-control" id="inputEmail" aria-describedby="emailHelp" placeholder="{{ app.user.email }}" required>
</div>
<div class="mb-3">
<label for="inputPassword">Password</label>
<input type="password" name="password" id="inputPassword" class="form-control" required>
</div>
<input type="hidden" name="_csrf_token"
value="{{ csrf_token('authenticate') }}"
>
<button class="btn btn-lg btn-primary" type="submit">
Save
</button>
</form>
</div>
<div class="mb-3">
<h3 class="mb-3">Delete Account</h3>
<button class="btn btn-danger">Delete Account</button>
</div>
{% endblock %}