Business
Technology
- Administration
- Android
- Apache Solr
- API
- Development Principles
- Django
- Eclipse
- Hibernate
- Javascript/jQuery
- Microsoft .NET
- Microsoft Office
- Patterns
- PHP
- Social Media
- Spring 3.0
- Spring Roo
- Symfony
- Symfony: Doctrine
- Symfony: View
- Symfony: Web Services
The fact that I have entered into IT-related business is proof that businesses have to evolve and keep with time. One has to re-invent continuously.
Kerry Packer
Symfony: Generate Google XML Sitemap
Looking for a Symfony Developer?
If you or your company are looking for a Symfony Developer contact us or read about our Symfony Development Service
Google XML Sitemaps are a vital part increasing your websites exposure on the web. In this article I show you how to use Symfony, the PHP framework, to generate a Google XML Sitemap.
Google provides lots of information for sitemap creation including XSD schema. To read more about the Google XML Sitemap read here. The Google XML Sitemap dosen't actually belong to Google however it has just kind of become the name to use when talking about XML Sitemaps. For a complete site dedicated to XML Sitemaps please refer to Sitemaps.org and for the XML Sitemap XSD see the Sitemaps Schema section.
The XML Sitemap XSD is really simple. There are only 6 elements in the whole XSD and only 3 of them are required. Below is a quick reference of the required elements.
- urlset - the root element for the sitemap,
- url - a container element for each url you want to index,
- loc - the actual url to index
For more information and the complete list please read the Sitemaps.org site.
Google allow you to specify the location of your XML Sitemap if you are using Google's Webmaster Tools. However if your not then Google will look for the http://yoursite.com/sitemap.xml file. This also goes for other search engines. So it is better if you just slip your XML Sitemap into your root directory. There are plenty of tools that will scrape your site and generate the XML sitemap for you. Then you simply have to copy that to your root directory.
With most management systems content is being added, edited and removed easily, quickly and frequently. The problem is your content management system is allowing your to deliver content to humans quickly and easily but hindering your efforts to notify the search engines of your changes.
Using Symfony you can automate the creation of the XML Sitemap in 4 easy steps.
- Create the route
- Create the sitemap module and xmlSitemap action
- Implement the action
- Create and implement the success template
There are 2 very important things to remember.
- Deliver the XML Sitemap in XML - Set the content type to text/xml
- Deliver the XML Sitemap without the standard layout decoration - sfAction::setLayout(false)
Following I will explain the above steps and to produce an actual example. This example makes the following assumptions:
- The Symfony application that will host the Sitemap module is called frontend
- The application has an Article model defined with a method that selects only the online articles
- You have implemented some form of URL re-writing in your web server to avoid exposing the "index.php" script
Create the Symfony XML Sitemap route
/apps/frontend/config/routing.xml
xml_sitemap:
url: /sitemap.xml
param: { module: sitemap, action: xmlSiteMap }
I name routes so that I know what it is for without having to read the param element.
We can see that the url sitemap.xml sits in the root of our application.
Create the Symfony Sitemap Module
Using the symfony script execute the following command
site-root#./symfony generate:module frontend sitemap
This will now create a new sitemap module in the frontend application
Open the newly created /apps/frontend/modules/sitemap/actions.class.php file and create a new action called generateXmlSitemap
public function generateXmlSitemap(sfRequest $request){
}
Implement the action
Add the following lines to the new method
Thanks: to mic for pointing out that generateXmlSiteMap should be a valid "action" name of executeXmlSiteMap
public function executeXmlSitemap(sfRequest $request){
$this->setLayout(false);
$this->getResponse()->setContentType('text/xml');
$this->articles = ArticleTable::getInstance()->getOnlineArticles();
}
This code stops the standard layout for the application being displayed durring the render stage.
Sets the response type to the required XML
Fetches all online articles in to our articles table.
Create and implement the xmlSitemapSuccess template
Finally create the following file and add the code below
/apps/frontend/sitemap/templates/xmlSitemapSuccess.php
Thanks: to mic for pointing out that the ; should have been a : in the foreach statement
<urlset xmlns="http://www.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">
<?php foreach($articles as $article): ?>
<url>
<loc><?php echo url_for('article/view?slug=' . $article->getSlug())?></loc>
<lastmod><?php echo $article->getDateTimeObject('updated_at')->format('c')?></lastmod>
</url>
<?php endforeach ?>
</urlset>
Add a comment

sebastian commented:
thank you!.
Minh Duc commented:
Best practice! Thks.
mic commented:
hi , you has tow error ,
public function generateXmlSitemap(sfRequest $request)
is executeXmlSitemap
second
<?php foreach($articles as $article); ?>
is : , not ;
Adam Pullen commented:
mic,
you are 100% correct.
Thanks
blogtj commented:
thank you for the tutorial.
ALso i think that it should be better if you would make the modifications in what mic proposed... i read only the code... and whithout the comments.... and it didn;t worked... fortunately i knew how to correct it.
Once more... great tutorial, thank you.
Kaore commented:
Nice, concise. Thanks!
For a site that has a large number of pages it can be interesting to cache the page every day or so to avoid to have to regenerate a large xml file every time a crawler visits it..
kaore - http://cibul.net