Adding an automatically-generated sitemap.xml to a Zend Framework site

This article looks at how to dynamically create a Google sitemap.xml file for your Zend Framework site. It assumes that you are using Zend_Navigation to describe the basic layout of your site. It also assumes that the application is based on Zend_Application and uses Zend_Layout.

First, we need to create the controller class used to field the request for the sitemap. This is called SitemapController and is located in controllers/SitemapController.php. The class has the following skeleton:

class SitemapController extends Zend_Controller_Action
{
  // TODO: Implement the class action handlers here
}

The first action is the index action, which is handled by the follow method of the class:

public function indexAction()
{
  $this->_helper->layout->disableLayout();
}

This disable the standard layout, so that only the XML of the sitemap is rendered.

The accompanying view script file is application/views/scripts/sitemap/index.phtml and contains the following code:

<?php
$this->navigation()->sitemap()->setFormatOutput(true); 
echo $this->navigation()->sitemap();

This takes the pages in the navigator and uses them to generate a sitemap.

Now, if you navigate to http://[your-domain]/sitemap, the sitemap will be rendered in the browser.

Now we need to set up the router, so that a request for http://[your-domain]/sitemap.xmlalso brings up the sitemap.

To do this, we should add the route to the application configuration file:

resources.router.routes.sitemap.type = "Zend_Controller_Router_Route_Static"
resources.router.routes.sitemap.route = "sitemap.xml"
resources.router.routes.sitemap.defaults.controller = "sitemap"
resources.router.routes.sitemap.defaults.action = "redirect"

We now need to add another action handler to the SitemapController class:

public function redirectAction()
{
  $this->_redirect('/sitemap');
}

The reason for doing this indirect redirection is that if we route directly to sitemap/index, the navigator becomes confused and renders all the URLs in the sitemap incorrectly. You can confirm this by changing the ini file setting above from “redirect” to “index”.

 

Fonte: http://zf.gm-ram.com/posts/adding-an-automatically-generated-sitemap-xml-to-a-zend-framework-site/

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair / Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair / Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair / Alterar )

Foto do Google+

Você está comentando utilizando sua conta Google+. Sair / Alterar )

Conectando a %s