Route requests for sitemap.xml to custom controller/action

By , Wednesday 6th January 2010 12:13 am

In order to direct requests for /sitemap.xml to a custom controller and action in your Zend Framework application simply add the following in your application.ini or alternative config file (e.g. I use navigation.ini):

resources.router.routes.sitemap.route                = "sitemap.xml"
resources.router.routes.sitemap.defaults.controller  = index
resources.router.routes.sitemap.defaults.action      = sitemap

Example code for outputting can be seen by creating an action in the appropriate controller (e.g. my sitemap lies in the index controller, sitemap action):

<php
class IndexController 
    extends Zend_Controller_Action
{
    /**
     * Renders a sitemap based on Zend_Navigation setup
     */
    public function sitemapAction()
    {
    	echo $this->view->navigation()->sitemap();
    	$this->view->layout()->disableLayout();
    	$this->_helper->viewRenderer->setNoRender(true);
    }
}

Sitemaps can quickly and easily be generated using Zend_Navigation, a great quick tutorial (and generally very useful for Zend Framework tutorials) is Zend CastsDynamically creating a menu a sitemap and breadcrumbs.

Liked this post? Follow this blog to get more. 

4 Responses to “Route requests for sitemap.xml to custom controller/action”

  1. One thing I forgot to mention is that you’ll also need to specify your standard route for your sitemap, otherwise all URLs will point to /sitemap.xml if you use Zend_Navigation_Page_Mvc (if you’re supplying URIs there won’t be a problem).

    For example my latest project uses the following routing:

    ; ==============================
    ; Resource Routing
    ; ==============================
    resources.router.routes.www.type = “Zend_Controller_Router_Route_Hostname”
    resources.router.routes.www.route = “:module.example.com”
    resources.router.routes.www.defaults.module = “default”
    resources.router.routes.www.chains.index.type = “Zend_Controller_Router_Route”
    resources.router.routes.www.chains.index.route = “:controller/:action/*”
    resources.router.routes.www.chains.index.defaults.controller = “index”
    resources.router.routes.www.chains.index.defaults.action = “index”
    resources.router.routes.sitemap.type = “Zend_Controller_Router_Route”
    resources.router.routes.sitemap.route = “sitemap.xml”
    resources.router.routes.sitemap.defaults.controller = index
    resources.router.routes.sitemap.defaults.action = sitemap

    Therefore in my navigation.xml (or whatever config you decide to use), you’ll need to specify the routing to use, e.g.

    default.pro.pages.aboutUs.label = “Company profile”
    default.pro.pages.aboutUs.module = “www”
    default.pro.pages.aboutUs.controller = “about-us”
    default.pro.pages.aboutUs.action = “index”
    default.pro.pages.aboutUs.resource = “www_pages”
    default.pro.pages.aboutUs.route = “www-index”

  2. […] a continuation on my last post about Zend_Navigation, Route requests for sitemap.xml to custom controller/action, this post is about dymnamically adding pages to a Zend_Navigation container at runtime/script […]

  3. Walter says:

    I have serious problems, I can’t resolve it :(

    I have router.ini

    resources.router.routes.sitemap.type = “Zend_Controller_Router_Route”
    resources.router.routes.sitemap.route = “sitemap.xml”
    resources.router.routes.sitemap.defaults.controller = index
    resources.router.routes.sitemap.defaults.action = sitemap

    and bootstrap.php

    protected function _initRouter()
    {
    $frontController = Zend_Controller_Front::getInstance();
    $config = new Zend_Config_Ini(APPLICATION_PATH . ‘/config/router.ini’);
    $router = $frontController->getRouter();
    $router->addConfig($config, ‘router’);
    }

    and I receive this error:

    Exception information:

    Message: Invalid controller specified (sitemap.xml)

    Request Parameters:

    array (
    ‘controller’ => ‘sitemap.xml’,
    ‘action’ => ‘index’,
    ‘module’ => ‘default’,
    )

    • It would seem that you’re missing a couple of entries, try updating your routes to match:

      resources.router.routes.sitemap.type = “Zend_Controller_Router_Route_Static”
      resources.router.routes.sitemap.route = “sitemap.xml”
      resources.router.routes.sitemap.controller = “index”
      resources.router.routes.sitemap.action = “sitemap”
      resources.router.routes.sitemap.defaults.module = “www”
      resources.router.routes.sitemap.defaults.controller = “index”
      resources.router.routes.sitemap.defaults.action = “sitemap”
      resources.router.routes.sitemap.defaults.route = “www-index”

Leave a Reply

You must be logged in to post a comment.

Panorama Theme by Themocracy

2 visitors online now
0 guests, 2 bots, 0 members
Max visitors today: 13 at 06:05 am UTC
This month: 16 at 18-07-2017 05:35 pm UTC
This year: 45 at 02-01-2017 10:28 pm UTC
All time: 130 at 28-03-2011 10:40 pm UTC