<?php
namespace App\Controller\Seo;
use App\Repository\BlogRepository;
use App\Repository\OutilRepository;
use App\Repository\WebinaireRepository;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class SeoController extends AbstractController
{
protected $domain;
protected $webinaires;
protected $outils;
protected $blogs;
public function __construct(WebinaireRepository $webinaireRepository, BlogRepository $blogRepository, OutilRepository $outilRepository)
{
$this->domain = "https://www.lagence.expert";
$this->webinaires = $webinaireRepository;
$this->blogs = $blogRepository;
$this->outils = $outilRepository;
}
#[Route('/sitemap.xml', name: 'seo_sitemapxml', defaults: ['_format' => 'xml'])]
public function sitemapxml(): Response
{
//$router = $this->container->get('router');
//$collection = $router->getRouteCollection();
//$allRoutes = $collection->all();
$webinaires_list = $this->webinaires->findBy(['online' => true]);
$outils_list = $this->outils->findBy(['online' => true]);
$blogs_list = $this->blogs->findBy(['online' => true]);
$urls = array(
'home' => '',
'primary' => array(),
'secondary' => array()
);
$primary_routes = array(
"agence-digitale-expert-comptable-paris",
"site-internet",
);
$secondary_routes = array(
"contact"
);
// url de base
$urls['home'] = $this->domain;
// urls primary
foreach ($primary_routes as $key => $value) {
array_push($urls['primary'], $this->sitemapUrl($value));
}
foreach ($blogs_list as $key => $value) {
array_push($urls['primary'], $this->sitemapUrl('singleBlog', array('id' => $value->getId())));
}
// urls secondary
foreach ($secondary_routes as $key => $value) {
array_push($urls['secondary'], $this->sitemapUrl($value));
}
return $this->render('seo/sitemap.html.twig', [
'urls' => $urls,
'lastmod' => date("Y-m-d"),
'changefreq' => 'monthly'
]);
}
private function sitemapUrl($path, $param = null)
{
try {
if (!is_null($param)) {
return $this->domain . $this->generateUrl($path, $param);
} else {
return $this->domain . $this->generateUrl($path);
}
} catch (\Throwable $th) {
return null;
}
}
#[Route('/robots.txt', name: 'seo_robotstxt', defaults: ['_format' => 'txt'])]
public function robotstxt(): Response
{
return $this->render('seo/robots.html.twig', [
'sitemap' => $this->domain . $this->generateUrl('seo_sitemapxml')
]);
}
}