From 14cf8d8fb5fe628abc9806f94aa18948553ece1f Mon Sep 17 00:00:00 2001 From: Pablo Rodriguez Date: Sun, 28 Jul 2024 02:51:44 -0400 Subject: [PATCH] Comment form can now upload image --- .gitignore | 1 + config/services.yaml | 1 + src/Controller/ConferenceController.php | 16 +++++++++++++--- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 4daae38..57dea93 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/public/uploads ###> symfony/framework-bundle ### /.env.local diff --git a/config/services.yaml b/config/services.yaml index 2d6a76f..b408ff3 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -4,6 +4,7 @@ # Put parameters here that don't need to change on each machine where the app is deployed # https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration parameters: + photo_dir: "%kernel.project_dir%/public/uploads/photos" services: # default configuration for services in *this* file diff --git a/src/Controller/ConferenceController.php b/src/Controller/ConferenceController.php index 12d4615..9c44487 100644 --- a/src/Controller/ConferenceController.php +++ b/src/Controller/ConferenceController.php @@ -9,6 +9,7 @@ use App\Repository\CommentRepository; use App\Repository\ConferenceRepository; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; @@ -30,14 +31,23 @@ class ConferenceController extends AbstractController } #[Route('/conference/{slug}', name: 'conference')] - public function show(Request $request, Conference $conference, CommentRepository $commentRepository): Response - { + public function show( + Request $request, + Conference $conference, + CommentRepository $commentRepository, + #[Autowire('%photo_dir%')] string $photoDir, + ): Response { $comment = new Comment(); $form = $this->createForm(CommentType::class, $comment); $form->handleRequest($request); if($form->isSubmitted() && $form->isValid()) { $comment->setConference($conference); - + if($photo = $form['photo']->getData()) { + $filename = bin2hex(random_bytes(6)) . '.' . $photo->guessExtension(); + $photo->move($photoDir, $filename); + $comment->setPhotoFilename($filename); + } + $this->entityManager->persist($comment); $this->entityManager->flush();