Simple xml Sitemap in Symfony 1.4

Symfony web development xmlThis article is mainly for beginner developers using Symfony Framework, we will guide you in your way of creating your dynamic XML website map.
For those of you who don’t know what xml Sitemap is we recommend that you read more about it at google.
If know what it is and you are wondering how to create it that follow this lesson. We are not going to get into all little details but never the less you will get a good introductory example in this matter.
To create a map first you need to have a web site with some content which you will describe. Let us say that you have a simple news site with all the news placed into different categories. Each category contains articles.The role of the dynamic map is to automatically extend itself noting newly added articles or categories.
Symfony web site xml
The first step is to create an empty module with the name site map. Than we go to routing.yml to create path. Here is an example:

url:   /xmlsitemap
param: { module: sitemap, action: xmlSitemap }

In the path we write the module name (the module that we just created) and the name of the template which we will create just now. In the folder templates inside the newly created module we will make a new file with the name xmlSitemapSuccess.php which we will leave empty for now. In the actions.class.php of the module we have to enter the following function representing the action toward our template:

public function executeXmlSitemap(sfRequest $request){
   $this->categories = Doctrine_Core::getTable('categories')->showSitemap();

In this line:

$this->categories = Doctrine_Core::getTable('categories')->showSitemap();

we call the function showSitemap() from the the class categoriesTable.class.php
The function has to look something like that:

public static function showSitemap() {
   $q = Doctrine_Query::create()
      ->from('categories c')
      ->leftJoin(' n)
      ->where(' = ?', 1);
   return $q->execute();

In this way we call all categories. With LeftJoin we call the news articles belonging to the categories and with where we can check if the news are active. Of course this is only an example. Now after we have created action which browses all categories and respective news it is time to fill in the file xmlSitemapSuccess.php which we left empty.

<urlset xmlns=""
    <loc><?php echo 'http://www.Вашия домейн'.url_for('index') ?></loc>
    <loc><?php echo 'http://www. Вашия домейн '.url_for('about') ?></loc>
    <loc><?php echo 'http://www. Вашия домейн '.url_for('contact') ?></loc>

In the first part of the file we describe the type of file. We also have 3 static links which are of pages with permanent content like About us or Contact. Then comes the dynamic part. By using foreach we bring up all categories. With the second foreach we bring up all the news belonging to the respective categorie. In the second cycle we direct the path toward the news in our website.

   <?php foreach($categories as $category): ?>
      <?php foreach($category->getNews() as $news):?>  
            <loc><?php echo 'http://www. Вашия домейн '.url_for('news',array('testId'=>$news->getId())) ?></loc>
            <lastmod><?php echo $news->getDateTimeObject('updated_at')->format('c')?></lastmod>
      <?php endforeach; ?>
   <?php endforeach; ?>

That is the general way of work. We hope you found the article useful. Now you know how to create a simple xml sitemap for your web site. For more information about the correct way of building the sitemap, the weight of the links etc. visit google.
symfony programming

Leave a Reply

Your email address will not be published. Required fields are marked *