Search code examples
phpzend-frameworksitemapzend-navigation

Zend_Navigation Sitemap not generating


I'm currently creating a project management system with the Zend Framework 1.12 and I got a problem with the Zend_Navigation::Sitemap() method.

I have a controller named SitemapController with indexAction() inside it who disable the layout. Then, my /views/scripts/sitemap/index.phtml script render the sitemap.

Problem, is that:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"/>

It's all I get in the /sitemap URI. Even if my navigation.xml is filled.

Here's all my code:

index.phtml

<?php
$this->navigation()->sitemap()->setFormatOutput(true)
                              ->setUseSchemaValidation(false)
                              ->setUseXmlDeclaration(true)
                              ->setUseSitemapValidators(true);
echo $this->navigation()->sitemap()->render($this->navigation);
?>

SitemapController.php

<?php
class SitemapController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

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

Bootstrap.php

/**
* @return Zend_Navigation
*/
protected function _initNavigation()
{
    $view = $this->bootstrap('layout')->getResource('layout')->getView();
    $config = new Zend_Config_Xml(APPLICATION_PATH.'/configs/navigation.xml', 'nav');
    $navigation = new Zend_Navigation($config);
    $view->navigation($navigation);
}

navigation.xml

<?xml version="1.0" encoding="UTF-8"?>
<configdata>
    <nav>
        <home>
            <label>Tableau de Bord</label>
            <controller>index</controller>
            <action>index</action>
        </home>
        <project>
            <label>Projets</label>
            <controller>project</controller>
            <action>index</action>
        </project>
        <tasks>
            <label>Tâches</label>
            <controller>tasks</controller>
            <action>index</action>
        </tasks>
        <messages>
            <label>Messages</label>
            <controller>messages</controller>
            <action>index</action>
        </messages>
    </nav>
</configdata>

Can someone tell me why it doesn't render the sitemap as it supposed to?


Solution

  • Is there any way to change the encoding in Zend_Navigation?

    I'd suggest to check encoding while you are constructing the menu tree or from your navigation.xml, perhaps.

    From Zend_Navigation documentation

    Note UTF-8 encoding used by default

    By default, Zend Framework uses UTF-8 as its default encoding, and, specific to this case, Zend\View does as well. Character encoding can be set differently on the view object itself using the setEncoding() method (or the the encoding instantiation parameter). However, since Zend\View\Interface does not define accessors for encoding, it’s possible that if you are using a custom view implementation with the Dojo view helper, you will not have a getEncoding() method, which is what the view helper uses internally for determining the character set in which to encode.

    If you do not want to utilize UTF-8 in such a situation, you will need to implement a getEncoding() method in your custom view implementation.

    It sometimes happens to me when dealing with ISO-8859-1 and JSON, that it just cuts the output, it occurs to me that might be something with your language.

    UPDATE

    This is the code I'm using in my sitemapAction:

    /**
     * Shows the site map.
     *
     * @return string
     */
    public function sitemapAction()
    {
        $this->view->layout()->disableLayout();
        $config = new Zend_Config_Xml(APPLICATION_PATH . DS . 'configs' . DS . 'navigation.xml', 'mainnav');
        $container = new Zend_Navigation($config);
        $this->view->navigation($container);
        $this->_helper->viewRenderer->setNoRender(true);
        $response = $this->getResponse();
        $response->setHeader('Content-Type', 'text/xml');
        echo $this->view->navigation()->sitemap();
    }