Comment form can now upload image
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
|
/public/uploads
|
||||||
|
|
||||||
###> symfony/framework-bundle ###
|
###> symfony/framework-bundle ###
|
||||||
/.env.local
|
/.env.local
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
# Put parameters here that don't need to change on each machine where the app is deployed
|
# 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
|
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
|
||||||
parameters:
|
parameters:
|
||||||
|
photo_dir: "%kernel.project_dir%/public/uploads/photos"
|
||||||
|
|
||||||
services:
|
services:
|
||||||
# default configuration for services in *this* file
|
# default configuration for services in *this* file
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ use App\Repository\CommentRepository;
|
|||||||
use App\Repository\ConferenceRepository;
|
use App\Repository\ConferenceRepository;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\Routing\Attribute\Route;
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
@@ -30,13 +31,22 @@ class ConferenceController extends AbstractController
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[Route('/conference/{slug}', name: 'conference')]
|
#[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();
|
$comment = new Comment();
|
||||||
$form = $this->createForm(CommentType::class, $comment);
|
$form = $this->createForm(CommentType::class, $comment);
|
||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
if($form->isSubmitted() && $form->isValid()) {
|
if($form->isSubmitted() && $form->isValid()) {
|
||||||
$comment->setConference($conference);
|
$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->persist($comment);
|
||||||
$this->entityManager->flush();
|
$this->entityManager->flush();
|
||||||
|
|||||||
Reference in New Issue
Block a user