Route requests for sitemap.xml to custom controller/action
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 Casts – Dynamically creating a menu a sitemap and breadcrumbs.
















































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