src/Controller/TagController.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\ArticleEntityService;
  4. use App\Service\TagEntityService;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. class TagController extends AbstractController
  9. {
  10.     /**
  11.      * @Route("/tags/{id}/articles/{page}", name="tags.articles", methods={"GET"}, defaults={"page": 1})
  12.      */
  13.     public function getArticles(Request $request,TagEntityService $entityServiceArticleEntityService $articleEntityService$id$page)
  14.     {
  15.         $tag $entityService->get($id);
  16.         if (!$tag || $tag->getDeletedAt() !== null) {
  17. //            return $this->json([
  18. //                'error' => 'tag.not_found',
  19. //                'message' => 'Tag not found',
  20. //            ]);
  21.             return $this->redirectToRoute('home');
  22.         }
  23.         $page = (int) $page;
  24.         $page max(1$page);
  25.         $limit 100;
  26.         $articles $articleEntityService->getArticlesByTag($tag$limit$page);
  27.         $count_articles = (int) $articleEntityService->countArticles($tag);
  28.         $pages_number ceil($count_articles $limit);
  29.         $isLastPage $page === $pages_number;
  30.         if ($request->isXmlHttpRequest()) {
  31.             $articles $articleEntityService->getArticlesByTag($tag$page 91);
  32.             return $this->render('themes/theme-5/_partials/article/tag-articles.html.twig', [
  33.                 'tag' => $tag,
  34.                 'count_articles' => $count_articles,
  35.                 'articles' => $articles,
  36.                 'page' => $page,
  37.                 'is_last_page' => $isLastPage,
  38.             ]);
  39.         }
  40.         return $this->render('/tags/tags.html.twig', [
  41.             'tag' => $tag,
  42.             'count_articles' => $count_articles,
  43.             'articles' =>  array_slice($articles09true),
  44.             'page' => $page,
  45.             'is_last_page' => $isLastPage,
  46.         ]);
  47.     }
  48. }