src/Controller/Frontend/SitemapController.php line 33
<?phpnamespace App\Controller\Frontend;use Webbamboo\SpinnersCommon\Entity\City;use Webbamboo\SpinnersCommon\Entity\Page;use Webbamboo\SpinnersCommon\Entity\Article;use Webbamboo\SpinnersCommon\Entity\Company;use Webbamboo\SpinnersCommon\Entity\Counties;use Webbamboo\SpinnersCommon\Entity\Generalsettings;use Doctrine\ORM\EntityManagerInterface;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;class SitemapController extends AbstractController{protected EntityManagerInterface $em;public function __construct(EntityManagerInterface $em){$this->em = $em;}private function getDTO(){$dt = new \DateTime();return $dt;}#[Route("/sitemaps/sitemap-general.xml", name: "spinner_general_sitemap", methods: ['GET'])]public function generalsitemapAction(){//var_dump($this->calculateCitySitemaps());ob_start();echo '<?xml version="1.0" encoding="UTF-8"?>';echo '<sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';//Countyecho '<sitemap>';echo '<loc>'.$this->generateUrl('spinner_county_sitemap', array(), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';echo '<lastmod>'.$this->getDTO()->createFromFormat('d-m-Y', '29-07-2014')->format('Y-m-d').'</lastmod>';echo '</sitemap>';//Cityfor ($i=1;$i<=$this->calculateCitySitemaps();$i++) {echo '<sitemap>';echo '<loc>'.$this->generateUrl('spinner_city_sitemap', array('part' => $i), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';echo '<lastmod>'.$this->getDTO()->createFromFormat('d-m-Y', '29-07-2014')->format('Y-m-d').'</lastmod>';echo '</sitemap>';}//Companyfor ($i=1;$i<=$this->calculateCompanySitemaps();$i++) {echo '<sitemap>';echo '<loc>'.$this->generateUrl('spinner_company_sitemap', array('part' => $i), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';echo '<lastmod>'.$this->getDTO()->createFromFormat('d-m-Y', '29-07-2014')->format('Y-m-d').'</lastmod>';echo '</sitemap>';}//Pageecho '<sitemap>';echo '<loc>'.$this->generateUrl('spinner_page_sitemap', array(), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';echo '<lastmod>'.$this->getDTO()->createFromFormat('d-m-Y', '29-07-2014')->format('Y-m-d').'</lastmod>';echo '</sitemap>';//echo '</sitemapindex>';//Articleecho '<sitemap>';echo '<loc>'.$this->generateUrl('spinner_article_sitemap', array(), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';echo '<lastmod>'.$this->getDTO()->createFromFormat('d-m-Y', '03-12-2014')->format('Y-m-d').'</lastmod>';echo '</sitemap>';echo '</sitemapindex>';$content = ob_get_contents();ob_end_clean();$response = new Response();$response->headers->set('Content-Type', 'text/xml');$response->setContent($content);return $response;}#[Route("/sitemaps/sitemap-county.xml", name: "spinner_county_sitemap", methods: ['GET'])]public function countysitemapAction(){$counties = $this->em->getRepository(Counties::class)->findAll();ob_start();echo '<?xml version="1.0" encoding="UTF-8"?>';echo ' <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';foreach ($counties as $c) {echo ' <url>';echo ' <loc>'.$this->generateUrl('spinner_front_county', array('countypermalink' => $this->capitalizePermalink($c->getPermalinkP())), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';echo ' <lastmod>'.$this->getDTO()->createFromFormat('d-m-Y', '29-07-2014')->format('Y-m-d').'</lastmod>';echo ' <changefreq>weekly</changefreq>';echo ' <priority>0.8</priority>';echo ' </url>';}echo ' </urlset>';$content = ob_get_contents();ob_end_clean();$response = new Response();$response->headers->set('Content-Type', 'text/xml');$response->setContent($content);return $response;}#[Route("/sitemaps/sitemap-pages.xml", name: "spinner_page_sitemap", methods: ['GET'])]public function pagesitemapAction(){$pages = $this->em->getRepository(Page::class)->findAll();ob_start();echo '<?xml version="1.0" encoding="UTF-8"?>';echo ' <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';foreach ($pages as $c) {echo ' <url>';echo ' <loc>'.$this->generateUrl('spinner_front_pages', array('pagepermalink' => $this->capitalizePermalink($c->getPermalinkP())), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';echo ' <lastmod>'.$this->getDTO()->createFromFormat('d-m-Y', '29-07-2014')->format('Y-m-d').'</lastmod>';echo ' <changefreq>weekly</changefreq>';echo ' <priority>0.8</priority>';echo ' </url>';}echo ' </urlset>';$content = ob_get_contents();ob_end_clean();$response = new Response();$response->headers->set('Content-Type', 'text/xml');$response->setContent($content);return $response;}#[Route("/sitemaps/sitemap-articles.xml", name: 'spinner_article_sitemap', methods: ['GET'])]public function articlesitemapAction(){$articles = $this->em->getRepository(Article::class)->findAll();ob_start();echo '<?xml version="1.0" encoding="UTF-8"?>';echo ' <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';foreach ($articles as $c) {echo ' <url>';echo ' <loc>'.$this->generateUrl('spinner_front_articles_single', array('articlepermalink' => $this->capitalizePermalink($c->getPermalinkP())), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';echo ' <lastmod>'.$this->getDTO()->createFromFormat('d-m-Y', '29-07-2014')->format('Y-m-d').'</lastmod>';echo ' <changefreq>weekly</changefreq>';echo ' <priority>0.8</priority>';echo ' </url>';}echo ' </urlset>';$content = ob_get_contents();ob_end_clean();$response = new Response();$response->headers->set('Content-Type', 'text/xml');$response->setContent($content);return $response;}#[Route("/sitemaps/sitemap-city-{part}.xml", name: "spinner_city_sitemap", methods: ['GET'])]public function citysitemapAction($part){$results = 1000;$realPart = $part-1;$offset = $realPart*$results;$cities = $this->em->getRepository(City::class)->findBy(array(), array(), $results, $offset);ob_start();echo '<?xml version="1.0" encoding="UTF-8"?>';echo ' <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';foreach ($cities as $c) {echo ' <url>';echo ' <loc>'.$this->generateUrl('spinner_front_city', array('citypermalink' => $this->capitalizePermalink($c->getPermalinkP()), 'countypermalink' => $this->capitalizePermalink($c->getCounty()->getPermalinkP())), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';echo ' <lastmod>'.$this->getDTO()->createFromFormat('d-m-Y', '29-07-2014')->format('Y-m-d').'</lastmod>';echo ' <changefreq>weekly</changefreq>';echo ' <priority>0.8</priority>';echo ' </url>';}echo ' </urlset>';$content = ob_get_contents();ob_end_clean();$response = new Response();$response->headers->set('Content-Type', 'text/xml');$response->setContent($content);return $response;}#[Route('/sitemaps/sitemap-company-{part}.xml', name: 'spinner_company_sitemap', methods: ['GET'])]public function companysitemapAction($part){$results = 1000;$realPart = $part-1;$offset = $realPart*$results;$companies = $this->em->getRepository(Company::class)->findBy(array(), array(), $results, $offset);ob_start();echo '<?xml version="1.0" encoding="UTF-8"?>';echo ' <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';foreach ($companies as $c) {$city = $c->getCityobj();if (isset($city)) {$county = $c->getCityobj()->getCounty();}if ($city && isset($county)) {echo ' <url>';echo ' <loc>'.$this->generateUrl('spinner_front_company', array('citypermalink' => $this->capitalizePermalink($city->getPermalinkP()), 'companypermalink' => $this->capitalizePermalink($c->getPermalinkP())), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';echo ' <lastmod>'.$this->getDTO()->createFromFormat('d-m-Y', '29-07-2014')->format('Y-m-d').'</lastmod>';echo ' <changefreq>weekly</changefreq>';echo ' <priority>0.8</priority>';echo ' </url>';}}echo ' </urlset>';$content = ob_get_contents();ob_end_clean();$response = new Response();$response->headers->set('Content-Type', 'text/xml');$response->setContent($content);return $response;}private function calculateCitySitemaps(){$qb = $this->em->createQueryBuilder();$qb->select('count(city.id)');$qb->from(City::class, 'city');$count = ceil($qb->getQuery()->getSingleScalarResult()/1000);return (int)$count;}private function calculateCompanySitemaps(){$qb = $this->em->createQueryBuilder();$qb->select('count(company.id)');$qb->from(Company::class, 'company');$count = ceil($qb->getQuery()->getSingleScalarResult()/1000);return (int)$count;}private function capitalizePermalink($permalink){return mb_convert_case($permalink, MB_CASE_TITLE, "UTF-8");}}