While tweaking the SunshinePHP site to be a bit more SEO friendly I realized we had neglected to create a sitemap for search engines to find. I was pleasantly surprised to see the Navigation component of Zend Framework 2 includes a bunch of view helpers, including a Sitemap helper. So now I have an xml sitemap created by Zend Framework 2 that works hand in hand with the site navigation. However, the documentation was not complete as of this writing and caused me to do a bit of trial and error debugging to get it working. Below I will post how I got it working, in hopes it will help others. (If the ZF2 folks like this post I will go in an update the documentation later.) As with most things in Zend Framework 2 there may be more than one way to do things, but this is how I did it. (Until someone informs me of a better way.)
Module Config
In the Application module.config.php I created a factories node in the service_manager container where I pulled in the DefaultNavigationFactory.
Then I also added a navigation container where I specified the sitemap for the site.
NOTE: To add navigation specific for each module you would simply create this container in the specific module.config.php.
Next I added a route for the future sitemap to be viewed. Notice how I simply added a sitemapAction to the Application IndexController. You can add it wherever you desire if you want to create a separate controller or whatever, I just left it there.
Layout
Because I just want the xml produced by the helper, I created a blank layout xml.phtml that does nothing more than output the content of the view.
View
The sitemap.phtml view is also pretty simple and outputs the xml sitemap created by the helper.
Controller
In my controller I specified the layout to use, nothing more was needed.
Verify
By navigation to the URL specified in the route we should now be able to view the XML output.
Future
In this example someone would need to navigate to /sitemap to view the sitemap, but some automated tools would try to go to /sitemap.xml which would fail with this setup. I will come back at some point in the future and enable the file extension to be ignored (after I figure out how).
Conclusion
The entire process is really pretty simple once the pieces are all in place, and the output was accepted by the various search engine webmaster tools…SCORE!