Symfony Simple Sitemap

<?php

namespace Brkhn\WebBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;

class SiteMapController extends Controller
{
    /**
     * @Route("/sitemap.{_format}", name="sitemaps_sitemap", Requirements={"_format" = "xml"})
     * @Template("BrkhnWebBundle:SiteMap:index.xml.twig")
     */
    public function indexAction()
    {
        $urls = array();
        $hostname = $this->getRequest()->getHost();

        $urls[] = array('loc' => trim($this->get('router')->generate('home_page'),'/'), 'changefreq' => 'weekly', 'priority' => '1.0');

        $questions = $this->getDoctrine()->getRepository('QuestionWebBundle:Posts');
        foreach($questions->findBy(array('typeId'=>1)) as $post ){
            $urls[] = array('loc' => $this->get('router')->generate('posts_view',
                    array('id' => $post->getId())), 'priority' => '0.5');
        }

        $users = $this->getDoctrine()->getRepository('UserUserBundle:User');
        foreach($users->findAll() as $user ){
            $urls[] = array('loc' => $this->get('router')->generate('user_profile',
                    array('id' => $user->getId())), 'priority' => '0.5');
        }
        return array('urls' => $urls, 'hostname' => $hostname);

    }

}
 
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    {% for url in urls %}
        <url>{# check if hostname is not alreay in url#}
            <loc>http://{%if url.loc|replace({hostname:''}) == url.loc%}{{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>

Çok fazla yazıya gerek olmasa gerek 🙂 kodlar konuşsun basitce bir sitemap xml oluşturmanıza yardımcı olur umarım.