src/Controller/ArticleController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Article;
  4. use App\Repository\ArticleRepository;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. /**
  10.  * @Route("/article")
  11.  */
  12. class ArticleController extends AbstractController
  13. {
  14.     /**
  15.      * @Route("/{slug}", name="article_show")
  16.      */
  17.     public function show(
  18.         Article $article,
  19.         ArticleRepository $articleRepo,
  20.         EntityManagerInterface $em
  21.     ): Response {
  22.         if (!$article->isPublished()) {
  23.             throw $this->createNotFoundException('Article non publiĆ©');
  24.         }
  25.         $article->incrementViewCount();
  26.         $em->flush();
  27.         $relatedArticles $articleRepo->findPublishedByCategory(
  28.             $article->getCategory(),
  29.             4
  30.         );
  31.         $relatedArticles array_filter($relatedArticles, function($a) use ($article) {
  32.             return $a->getId() !== $article->getId();
  33.         });
  34.         return $this->render('article/show.html.twig', [
  35.             'article' => $article,
  36.             'related_articles' => array_slice($relatedArticles03),
  37.         ]);
  38.     }
  39.     /**
  40.      * @Route("/preview/{id}", name="article_preview")
  41.      */
  42.     public function preview(Article $article): Response
  43.     {
  44.         $this->denyAccessUnlessGranted('ROLE_ADMIN');
  45.         return $this->render('article/show.html.twig', [
  46.             'article' => $article,
  47.             'is_preview' => true,
  48.             'related_articles' => [],
  49.         ]);
  50.     }
  51. }