src/Controller/Frontend/NewsController.php line 188

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use FOS\RestBundle\Controller\Annotations as Rest;
  5. use App\Misc\ImageUrlHelper;
  6. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  7. use Symfony\Contracts\Translation\TranslatorInterface;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. class NewsController extends BaseFrontendController
  10. {
  11.     /**
  12.      * @Route("/{_locale}/actualites", requirements={"_locale": "en|fr"}, defaults={"_locale"="fr"}, name="news.index")
  13.      */
  14.     public function index(
  15.         UrlGeneratorInterface $urlGenerator,
  16.         ImageUrlHelper $imageUrlHelper,
  17.         TranslatorInterface $translator
  18.     ) {
  19.         $requestData['limit'] = 11;
  20.         $news $this->currentService->getListAll($requestData);
  21.         $siteName $translator->trans('siteName');
  22.         $metaTitle $translator->trans(
  23.             'news.title'
  24.         );
  25.         $this->addTitle($metaTitle);
  26.         $this->setDescription($translator->trans('news.desc'));
  27.         // BreadcrumbSnippet
  28.         $breadcrumbSnippet = [
  29.             '@context'        => 'https://schema.org',
  30.             '@type'           => 'BreadcrumbList',
  31.             'itemListElement' => [
  32.                 [
  33.                     '@type'    => 'ListItem',
  34.                     'position' => 1,
  35.                     'name'     => $siteName,
  36.                     'item'     => $urlGenerator->generate(
  37.                         'home',
  38.                         [],
  39.                         UrlGeneratorInterface::ABSOLUTE_URL
  40.                     )
  41.                 ],
  42.                 [
  43.                     '@type'    => 'ListItem',
  44.                     'position' => 2,
  45.                     'name'     => $metaTitle,
  46.                 ],
  47.             ]
  48.         ];
  49.         // Article snippet
  50.         $pageModifyDate = new \DateTime('first day of this month');
  51.         $newsArticleRichSnippet = [
  52.             '@context'      => 'https://schema.org',
  53.             '@type'         => 'NewsArticle',
  54.             'headline'      => $metaTitle,
  55.             'image'         => [
  56.                 $imageUrlHelper->getNewsImageUrl()
  57.             ],
  58.             'datePublished' => '2022-09-01 00:00:00',
  59.             'dateModified'  => $pageModifyDate->format('Y-m-d') . ' 00:00:00',
  60.             'author'        => [
  61.                 '@type' => 'Organization',
  62.                 'name'  => 'Estiam France',
  63.                 'url'   => $urlGenerator->generate(
  64.                     'home',
  65.                     [],
  66.                     UrlGeneratorInterface::ABSOLUTE_URL
  67.                 )
  68.             ],
  69.         ];
  70.         $data['news'] = $news;
  71.         $data['richSnippets'] = [];
  72.         $data['richSnippets'][] = $breadcrumbSnippet;
  73.         $data['richSnippets'][] = $newsArticleRichSnippet;
  74.         return $this->render(
  75.             'news/index.html.twig',
  76.             $data
  77.         );
  78.     }
  79.     /**
  80.      * @Rest\Get("/load-more", name="news.loadMore")
  81.      */
  82.     public function loadMore(
  83.         Request $request
  84.     ) {
  85.         $requestData $request->query->all();
  86.         $requestData['initOffset'] = 11;
  87.         $news $this->currentService->getListAll($requestData);
  88.         if (!$news) {
  89.             return $this->json([
  90.                 'data' => []
  91.             ]);
  92.         }
  93.         $data['list'] = $this->renderView(
  94.             'news/_load_more.html.twig',
  95.             [
  96.                 'newsMore' => $news
  97.             ]
  98.         );
  99.         return $this->json($data);
  100.     }
  101.     /**
  102.      * @Route("/{_locale}/actualites/{slug}-{id}", requirements={"_locale": "en|fr", "slug": "[a-zA-Z0-9\-]+","id": "[0-9]+"}, defaults={"_locale"="fr"}, name="news.detail")
  103.      */
  104.     public function detail(
  105.         $slug,
  106.         $id,
  107.         Request $request,
  108.         UrlGeneratorInterface $urlGenerator,
  109.         ImageUrlHelper $imageUrlHelper,
  110.         TranslatorInterface $translator
  111.     ) {
  112.         $news $this->currentService->getBySlug($slug$request);
  113.         if (!$news) throw $this->createNotFoundException('');
  114.         $siteName $translator->trans('siteName');
  115.         $this->addTitle($news->seoTitle);
  116.         $this->setDescription($news->seoDescription);
  117.         // Breadcrumb snippet
  118.         $breadcrumbSnippet = [
  119.             '@context'        => 'https://schema.org',
  120.             '@type'           => 'BreadcrumbList',
  121.             'itemListElement' => [
  122.                 [
  123.                     '@type'    => 'ListItem',
  124.                     'position' => 1,
  125.                     'name'     => $news->seoTitle,
  126.                     'item'     => $urlGenerator->generate(
  127.                         'home',
  128.                         [],
  129.                         UrlGeneratorInterface::ABSOLUTE_URL
  130.                     )
  131.                 ],
  132.                 [
  133.                     '@type'    => 'ListItem',
  134.                     'position' => 2,
  135.                     'name'     => $siteName
  136.                 ]
  137.             ]
  138.         ];
  139.         // NewsArticle richSnippet ------------------------------------
  140.         $newsArticleRichSnippet = [
  141.             '@context'      => 'https://schema.org',
  142.             '@type'         => 'NewsArticle',
  143.             'headline'      => $news->seoTitle,
  144.             'image'         => [
  145.                 $news->photo ?? $imageUrlHelper->getNewsImageUrl()
  146.             ],
  147.             'datePublished' => $news->createdDate,
  148.             'dateModified'  => $news->updatedDate,
  149.             'author'        => [
  150.                 '@type' => 'Organization',
  151.                 'name'  => $siteName,
  152.                 'url'   => $urlGenerator->generate(
  153.                     'home',
  154.                     [],
  155.                     UrlGeneratorInterface::ABSOLUTE_URL
  156.                 )
  157.             ],
  158.         ];
  159.         $data['news'] = $news;
  160.         $data['newsLasted'] = $this->currentService->getListLasted($news->id);
  161.         $data['nextNews'] = $this->newsRepo->getNextNews($news->id);
  162.         $data['richSnippets'] = [];
  163.         $data['richSnippets'][] = $breadcrumbSnippet;
  164.         $data['richSnippets'][] = $newsArticleRichSnippet;
  165.         return $this->render(
  166.             'news/detail.html.twig',
  167.             $data
  168.         );
  169.     }
  170. }