src/Controller/QuestionController.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Question;
  4. use App\Repository\QuestionRepository;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Psr\Log\LoggerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class QuestionController extends AbstractController
  12. {
  13.     private $logger;
  14.     private $isDebug;
  15.     public function __construct(LoggerInterface $loggerbool $isDebug)
  16.     {
  17.         $this->logger $logger;
  18.         $this->isDebug $isDebug;
  19.     }
  20.     #[Route('/'name'app_homepage')]
  21.     public function homepage(QuestionRepository $repository)
  22.     {
  23.         $questions $repository->findAllApprovedOrderedByNewest();
  24.         return $this->render('question/homepage.html.twig', [
  25.             'questions' => $questions,
  26.         ]);
  27.     }
  28.     #[Route('/questions/new')]
  29.     public function new()
  30.     {
  31.         return new Response('Sounds like a GREAT feature for V2!');
  32.     }
  33.     #[Route('/questions/{slug}'name'app_question_show')]
  34.     public function show(Question $question)
  35.     {
  36.         if (!$question->getIsApproved()) {
  37.             throw $this->createNotFoundException(sprintf('Question %s has not been approved yet'$question->getId()));
  38.         }
  39.         if ($this->isDebug) {
  40.             $this->logger->info('We are in debug mode!');
  41.         }
  42.         return $this->render('question/show.html.twig', [
  43.             'question' => $question,
  44.         ]);
  45.     }
  46.     #[Route('/questions/{slug}/vote'name'app_question_vote'methods'POST')]
  47.     public function questionVote(Question $questionRequest $requestEntityManagerInterface $entityManager)
  48.     {
  49.         $direction $request->request->get('direction');
  50.         if ($direction === 'up') {
  51.             $question->upVote();
  52.         } elseif ($direction === 'down') {
  53.             $question->downVote();
  54.         }
  55.         $entityManager->flush();
  56.         return $this->redirectToRoute('app_question_show', [
  57.             'slug' => $question->getSlug()
  58.         ]);
  59.     }
  60. }