<?php
namespace FoundersBundle\Controller;
use Doctrine\ORM\EntityManagerInterface;
use FoundersBundle\Entity\Biography;
use FoundersBundle\Entity\CategoryPost;
use FoundersBundle\Entity\Post;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Yaml\Yaml;
class SiteMapController extends BaseController
{
public function __construct(EntityManagerInterface $em)
{
parent::__construct($em);
}
public function siteMap(RouterInterface $router): Response
{
$root = $this->getParameter('kernel.project_dir');
$routingFile = $root . '/src/FoundersBundle/Resources/config/routing/sitemap.yml';
$routes = Yaml::parseFile($routingFile);
$result = [];
foreach ($routes as $rName => $route) {
if ($rName == 'founders_homepage') {
$result[] = [
'url' => $router->generate($rName),
'priority' => '1',
'changefreq' => 'daily',
];
}
if (in_array(
$rName, [
'founders_family_philosophy_get_posts',
'founders_family_biographies',
'founders_investment_category',
'founders_investment_social_single',
'founders_media_posts_single',
'founders_homepage',
])) {
continue;
}
$result[] = [
'url' => $router->generate($rName),
'priority' => '0.5',
'changefreq' => 'daily',
];
}
$biographies = $this->em->getRepository(Biography::class)->findAll();
foreach ($biographies as $biography) {
$result[] = [
'url' => $router->generate('founders_family_biographies', ['url' => $biography->getUrl()]),
'priority' => '0.8',
'changefreq' => 'daily',
];
}
$investmentCategories = $this->em->getRepository(CategoryPost::class)->findBy(['state' => true]);
foreach ($investmentCategories as $category) {
$result[] = [
'url' => $router->generate('founders_investment_category', ['category' => $category->getUrl()]),
'priority' => '0.5',
'changefreq' => 'daily',
];
}
$investmentPosts = $this->em->getRepository(Post::class)->getAllInvestmentPosts();
foreach ($investmentPosts as $post) {
$result[] = [
'url' => $router->generate('founders_investment_social_single',
['category' => $post->getCategory()->getUrl(), 'url' => $post->getUrl()]),
'priority' => '0.5',
'changefreq' => 'daily',
];
}
$allPosts = $this->em->getRepository(Post::class)->getAllMediaPosts();
foreach ($allPosts as $post) {
$result[] = [
'url' => $router->generate('founders_media_posts_single',
['category' => $post->getCategoryUrlById(), 'url' => $post->getUrl()]),
'priority' => '0.6',
'changefreq' => 'daily',
];
}
$response = $this->baseFounderController('@Founders/sitemap.xml.twig', ['urls' => $result, 'route' => 'founders_sitemap']);
$response->headers->set('Content-Type', 'application/xml');
$response->setCharset('utf-8');
return $response;
}
public function robots(): Response
{
$content = $this->renderView('@Founders/robots.txt.twig', []);
return new Response($content, Response::HTTP_OK, ['Content-Type' => 'text/plain']);
}
}