Skip to content

Instantly share code, notes, and snippets.

@MaximStrutinskiy
Last active June 1, 2018 16:22
Show Gist options
  • Save MaximStrutinskiy/ec66d06a333fb15d01d85ec28cbaae2d to your computer and use it in GitHub Desktop.
Save MaximStrutinskiy/ec66d06a333fb15d01d85ec28cbaae2d to your computer and use it in GitHub Desktop.
#Sitemap
sitemap:
path: /sitemap.xml
defaults: { _controller: MainBundle:Customer/Sitemap:index }
<?xml version="1.0" encoding="UTF-8" ?>
<urlset
xmlns="http://sitemaps.org/schemas/sitemap/0.9"
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/sitemap.xsd">
{% for url in urls %}
<url>
{# check if hostname is not already in url #}
<loc>{% if url.loc|replace({hostname:''}) == url.loc %}http://{{hostname}}{{url.loc}}{% else %}{{url.loc}}{% endif %}</loc>
{% if url.lastmod is defined %}
<lastmod>{{ url.lastmod }}</lastmod>
{% endif %}
{% if url.changefreq is defined %}
<changefreq>{{ url.changefreq }}</changefreq>
{% endif %}
{% if url.priority is defined %}
<priority>{{ url.priority }}</priority>
{% endif %}
</url>
{% endfor %}
</urlset>
<?php
namespace MainBundle\Controller\Customer;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Class SitemapController
* @package MainBundle\Controller\Customer
*/
class SitemapController extends Controller
{
/**
*
*/
public function indexAction(Request $request)
{
$urls = [];
$hostname = $request->getHost();
$urls[] = ['loc' => $this->get('router')->generate('index'), 'changefreq' => 'weekly', 'priority' => '1.0'];
$urls[] = ['loc' => $this->get('router')->generate('index'), 'changefreq' => 'weekly', 'priority' => '1.0'];
// Then, we will find all our articles stored in the database
$articlesRepository = $this->getDoctrine()->getRepository('MainBundle:Post');
$articles = $articlesRepository->findAll();
// We loop on them
foreach ($articles as $article) {
$urls[] = ['loc' => $this->get('router')->generate('post', ['id' => $article->getId()]), 'changefreq' => 'weekly', 'priority' => '1.0'];
}
// Once our array is filled, we define the controller response
$response = new Response();
$response->headers->set('Content-Type', 'xml');
$response = new Response($this->renderView('@Main/Customer/page/sitemap.xml.twig', [
'urls' => $urls,
'hostname' => $hostname
]));
$response->headers->set('Content-Type', 'application/xml; charset=utf-8');
return $response;
}
}
<html><head></head><body><urlset xmlns="http://sitemaps.org/schemas/sitemap/0.9" 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/sitemap.xsd">
<url>
<loc>http://test.ru/</loc>
<changefreq>weekly</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>http://test.ru/</loc>
<changefreq>weekly</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>http://test.ru/post/2</loc>
<changefreq>weekly</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>http://test.ru/post/3</loc>
<changefreq>weekly</changefreq>
<priority>1.0</priority>
</url>
</urlset></body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment