Adding an automatically-generated sitemap.xml to a Zend Framework site
Saturday, August 21, 2010
This article looks at how to dynamically create a Google sitemap.xml file for your Zend Framework site. It assumes that you are using Zend_Navigation to describe the basic layout of your site. It also assumes that the application is based on Zend_Application and uses Zend_Layout.
First, we need to create the controller class used to field the request for the sitemap. This is called SitemapController and is located in controllers/SitemapController.php. The class has the following skeleton:
class SitemapController extends Zend_Controller_Action { // TODO: Implement the class action handlers here }
The first action is the index action, which is handled by the follow method of the class:
public function indexAction() { $this->_helper->layout->disableLayout(); }
This disable the standard layout, so that only the XML of the sitemap is rendered.
The accompanying view script file is application/views/scripts/sitemap/index.phtml and contains the following code:
<?php $this->navigation()->sitemap()->setFormatOutput(true); echo $this->navigation()->sitemap();
This takes the pages in the navigator and uses them to generate a sitemap.
Now, if you navigate to http://[your-domain]/sitemap, the sitemap will be rendered in the browser.
Now we need to set up the router, so that a request for http://[your-domain]/sitemap.xml also brings up the sitemap.
To do this, we should add the route to the application configuration file:
resources.router.routes.sitemap.type = "Zend_Controller_Router_Route_Static" resources.router.routes.sitemap.route = "sitemap.xml" resources.router.routes.sitemap.defaults.controller = "sitemap" resources.router.routes.sitemap.defaults.action = "redirect"
We now need to add another action handler to the SitemapController class:
public function redirectAction() { $this->_redirect('/sitemap'); }
The reason for doing this indirect redirection is that if we route directly to sitemap/index, the navigator becomes confused and renders all the URLs in the sitemap incorrectly. You can confirm this by changing the ini file setting above from “redirect” to “index”.

Practical Web 2.0 Applications with PHP (Expert's Voice) by Quentin Zervaas
Beginning Databases with PostreSQL: From Expert to Professional 2nd Edition: From Novice to Professional by Neil Matthew, Richard Stones
[...] http://zf.gm-ram.com/posts/adding-an-automatically-generated-sitemap-xml-to-a-zend-framework-site/ [...]
[...] to the site; see the post on pagination, the post on a plugin to populate the registry and the post on automatically generating a sitemap. I intend to do some further posts on the new parts of the site implementation that are of wider [...]
Great article, thanks. The only thing I would add is that you should have the following in the controller to set the content type header correctly:
$this->getResponse()->setHeader(‘Content-Type’, ‘text/xml’);
I also didn’t run into the bug you mentioned necessitating the redirect. Maybe this has now been fixed? I’m using ZF 1.11.11.