render('conference/index.html.twig', [ 'conferences' => $conferenceRepository->findAll(), ]); } #[Route('/conference/{slug}', name: 'conference')] public function show( Request $request, Conference $conference, CommentRepository $commentRepository, SpamChecker $spamChecker, ConferenceRepository $conferenceRepository, #[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); $context = [ 'user_ip' => $request->getClientIp(), 'user_agent' => $request->headers->get('user-agent'), 'referrer' => $request->headers->get('referer'), 'permalink' => $request->getUri(), ]; if (2 === $spamChecker->getSpamScore($comment, $context)) { throw new \RuntimeException('Blatant spam, go away!'); } $this->entityManager->flush(); return $this->redirectToRoute('conference', ['slug' => $conference->getSlug()]); } $offset = max(0, $request->query->getInt('offset', 0)); $paginator = $commentRepository->getCommentPaginator($conference, $offset); return $this->render('conference/show.html.twig', [ 'conferences' => $conferenceRepository->findAll(), 'conference' => $conference, 'comments' => $paginator, 'previous' => $offset - CommentRepository::COMMENTS_PER_PAGE, 'next' => min(count($paginator), $offset + CommentRepository::COMMENTS_PER_PAGE), 'comment_form' => $form, ]); } }