src/Controller/Frontend/ActualiteController.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use App\Entity\Article;
  9. use App\Repository\ArticleRepository;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Knp\Component\Pager\PaginatorInterface;
  12. use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
  13. class ActualiteController extends AbstractController
  14. {
  15. /**
  16. * @Route("/actualite-{slug}-{id}.html", name="actualite")
  17. */
  18. public function actualite(EntityManagerInterface $em, $id, Request $request,UploaderHelper $helper): Response
  19. {
  20. $article = $em->getRepository(Article::class)->findOneById($id);
  21. if($article == null || $article->getIsActive() == false)
  22. throw $this->createNotFoundException('La page n\'existe pas');
  23. $img_path = $helper->asset($article);
  24. $text = html_entity_decode(str_replace(array("\r", "\n"), '',strip_tags($article->getText())), ENT_QUOTES | ENT_HTML5, 'UTF-8');
  25. $jsonLd = [
  26. "@context"=> "https://schema.org",
  27. "@type"=> "BlogPosting",
  28. "headline"=> $article->getTitle(),
  29. "image"=> "https://{$_SERVER['HTTP_HOST']}{$img_path}",
  30. "wordcount"=> str_word_count($text),
  31. "publisher"=> "Croqvacances",
  32. "url"=> $request->getUri(),
  33. "datePublished"=> $article->getCreationDate()->format('Y-m-d'),
  34. "description"=> substr($text, 0, 100),
  35. "articleBody"=> $text,
  36. ];
  37. $jsonLdScript = sprintf('<script type="application/ld+json">%s</script>', json_encode($jsonLd, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
  38. return $this->render('frontend/actualite/showArticle.html.twig',['article' => $article,'jsonLdScript' => $jsonLdScript,]);
  39. }
  40. /**
  41. * @Route("/actualites", name="frontend_actualites",options={"sitemap" = true})
  42. */
  43. public function actualites(ArticleRepository $ar, PaginatorInterface $paginator, EntityManagerInterface $em, Request $request): Response{
  44. $dql = "SELECT a FROM App\Entity\Article a WHERE a.isActive = 1 AND a.type = 'article' ORDER BY a.creationDate DESC";
  45. $query = $em->createQuery($dql);
  46. $pagination = $paginator->paginate(
  47. $query, /* query NOT result */
  48. $request->query->getInt('page', 1), /* page number */
  49. 6 /* limit per page */
  50. );
  51. return $this->render('frontend/actualite/articles.html.twig',['pagination' => $pagination]);
  52. }
  53. }