src/Controller/Frontend/AlumniController.php line 102

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 AlumniController extends BaseFrontendController
  10. {
  11.     /**
  12.      * @Route("/{_locale}/alumni", requirements={"_locale": "en|fr"}, defaults={"_locale"="fr"}, name="alumni_index")
  13.      */
  14.     public function index(
  15.         UrlGeneratorInterface $urlGenerator,
  16.         ImageUrlHelper $imageUrlHelper,
  17.         TranslatorInterface $translator
  18.     ) {
  19.         $requestData['limit'] = 10000;
  20.         $alumni $this->currentService->getListAll($requestData);
  21.         $siteName $translator->trans('siteName');
  22.         $metaTitle $translator->trans(
  23.             'alumni.title',
  24.             [
  25.                 '%siteName%' => $siteName
  26.             ]
  27.         );
  28.         $this->addTitle($metaTitle);
  29.         $this->setDescription($translator->trans(
  30.             'alumni.desc',
  31.             [
  32.                 '%siteName%' => $siteName
  33.             ]
  34.         ));
  35.         // BreadcrumbSnippet
  36.         $breadcrumbSnippet = [
  37.             '@context'        => 'https://schema.org',
  38.             '@type'           => 'BreadcrumbList',
  39.             'itemListElement' => [
  40.                 [
  41.                     '@type'    => 'ListItem',
  42.                     'position' => 1,
  43.                     'name'     => $siteName,
  44.                     'item'     => $urlGenerator->generate(
  45.                         'home',
  46.                         [],
  47.                         UrlGeneratorInterface::ABSOLUTE_URL
  48.                     )
  49.                 ],
  50.                 [
  51.                     '@type'    => 'ListItem',
  52.                     'position' => 2,
  53.                     'name'     => $metaTitle,
  54.                 ],
  55.             ]
  56.         ];
  57.         // Article snippet
  58.         $pageModifyDate = new \DateTime('first day of this month');
  59.         $newsArticleRichSnippet = [
  60.             '@context'      => 'https://schema.org',
  61.             '@type'         => 'NewsArticle',
  62.             'headline'      => $metaTitle,
  63.             'image'         => [
  64.                 $imageUrlHelper->getNewsImageUrl()
  65.             ],
  66.             'datePublished' => '2022-09-01 00:00:00',
  67.             'dateModified'  => $pageModifyDate->format('Y-m-d') . ' 00:00:00',
  68.             'author'        => [
  69.                 '@type' => 'Organization',
  70.                 'name'  => 'Estiam France',
  71.                 'url'   => $urlGenerator->generate(
  72.                     'home',
  73.                     [],
  74.                     UrlGeneratorInterface::ABSOLUTE_URL
  75.                 )
  76.             ],
  77.         ];
  78.         $data['alumni'] = $alumni;
  79.         $data['richSnippets'] = [];
  80.         $data['richSnippets'][] = $breadcrumbSnippet;
  81.         $data['richSnippets'][] = $newsArticleRichSnippet;
  82.         return $this->render(
  83.             'alumni/index.html.twig',
  84.             $data
  85.         );
  86.     }
  87.     /**
  88.      * @Rest\Get("/{_locale}/{id}-{slug}", requirements={"_locale": "en|fr", "slug": "[a-zA-Z0-9\-]+", "id": "[0-9]+"}, defaults={"_locale"="fr"}, name="alumni_detail")
  89.      */
  90.     public function detail(
  91.         $slug,
  92.         Request $request,
  93.         UrlGeneratorInterface $urlGenerator,
  94.         ImageUrlHelper $imageUrlHelper
  95.     ) {
  96.         $alumni $this->currentService->getBySlug($slug$request);
  97.         if (!$alumni) throw $this->createNotFoundException('');
  98.         $siteName $this->getParameter('site_name');
  99.         $this->addTitle($alumni->seoTitle);
  100.         $this->setDescription($alumni->seoDescription);
  101.         // Breadcrumb snippet
  102.         $breadcrumbSnippet = [
  103.             '@context'        => 'https://schema.org',
  104.             '@type'           => 'BreadcrumbList',
  105.             'itemListElement' => [
  106.                 [
  107.                     '@type'    => 'ListItem',
  108.                     'position' => 1,
  109.                     'name'     => $alumni->seoTitle,
  110.                     'item'     => $urlGenerator->generate(
  111.                         'home',
  112.                         [],
  113.                         UrlGeneratorInterface::ABSOLUTE_URL
  114.                     )
  115.                 ],
  116.                 [
  117.                     '@type'    => 'ListItem',
  118.                     'position' => 2,
  119.                     'name'     => $siteName
  120.                 ]
  121.             ]
  122.         ];
  123.         // NewsArticle richSnippet ------------------------------------
  124.         $newsArticleRichSnippet = [
  125.             '@context'      => 'https://schema.org',
  126.             '@type'         => 'NewsArticle',
  127.             'headline'      => $alumni->seoTitle,
  128.             'image'         => [
  129.                 $alumni->photo ?? $imageUrlHelper->getLogoUrl()
  130.             ],
  131.             'datePublished' => $alumni->createdDate,
  132.             'dateModified'  => $alumni->updatedDate,
  133.             'author'        => [
  134.                 '@type' => 'Organization',
  135.                 'name'  => $siteName,
  136.                 'url'   => $urlGenerator->generate(
  137.                     'home',
  138.                     [],
  139.                     UrlGeneratorInterface::ABSOLUTE_URL
  140.                 )
  141.             ],
  142.         ];
  143.         $data['alumni'] = $alumni;
  144.         $data['richSnippets'] = [];
  145.         $data['richSnippets'][] = $breadcrumbSnippet;
  146.         $data['richSnippets'][] = $newsArticleRichSnippet;
  147.         return $this->render(
  148.             'alumni/detail.html.twig',
  149.             $data
  150.         );
  151.     }
  152. }