src/Controller/QuestionController.php line 27

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