Zend Framework 2 XML Sitemap

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.

factory

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.

navigation

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.

route

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.

layout

View

The sitemap.phtml view is also pretty simple and outputs the xml sitemap created by the helper.

view

Controller

In my controller I specified the layout to use, nothing more was needed.

controller

Verify

By navigation to the URL specified in the route we should now be able to view the XML output.

sitemap

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!

Published by

Adam

Application Architect from Florida

2 thoughts on “Zend Framework 2 XML Sitemap”

  1. You can use Soflomo\Prototype for this feature to load a sitemap by just enabling it in your application.config.php. The module is available on GitHub at https://github.com/Soflomo/Sitemap

    Currently, the HTML sitemap is available under /sitemap and the XML version is /sitemap/xml. But if you like to support /sitemap.xml too, feel free to PR the source!

  2. great post, btw, you don’t need new empty layout for it, you can use :

    use Zend\View\Model\ViewModel;
    
        public function sitemapAction()
        {
             $viewmodel = new ViewModel;
             $viewmodel->setTerminal(true);
    
             return $viewmodel;
        }
    

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>