Route requests for sitemap.xml to custom controller/action

By Steven Lloyd Watkin, 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.

Post to Twitter Tweet This Post Post to Plurk Plurk This Post Post to Yahoo Buzz Buzz This Post Post to Delicious Delicious Post to Digg Digg This Post Post to Facebook Facebook Post to MySpace MySpace Post to Ping.fm Ping This Post Post to Reddit Reddit Post to StumbleUpon Stumble This Post

2 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 [...]

Leave a Reply













Panorama Theme by Themocracy

6 visitors online now
2 guests, 4 bots, 0 members
Max visitors today: 12 at 12:26 am UTC
This month: 19 at 07-09-2010 10:45 am UTC
This year: 66 at 15-07-2010 02:49 am UTC
All time: 66 at 15-07-2010 02:49 am UTC