ZF-11448: Problem with sitemap generation

Description

I'm trying to create sitemap within controller. Controller code is:


<?php
class SitemapController extends Zend_Controller_Action
{
    function xmlAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();
        $this->getResponse()->setHeader('Content-Type', 'text/xml; charset=utf-8');
        
        echo $this->view->navigation()->sitemap();
    }
}
I want to view the same sitemap in http://localhost/sitemap.xml and http://localhost/sitemap/xml. For this I'm creating route in bootstrap:

protected function _initFrontController() {
    $front = Zend_Controller_Front::getInstance();
    $front->setControllerDirectory(
            array(
        'default'=>APPLICATION_PATH.'/modules/default/controllers',
        'admin'=>APPLICATION_PATH.'/modules/admin/controllers',
        )
    );
    $router = $front->getRouter();
    $router->addRoute(
        'sitemap',
        new Zend_Controller_Router_Route('sitemap.xml', array('controller'=>'sitemap','action'=>'xml','module'=>'default'))
    );
    $front->setRouter($router);
    
    $front->registerPlugin(new Zend_Controller_Plugin_ErrorHandler());
    return $front;
}
But result is different. For /sitemap/xml I see:

http://127.0.0.1/http://127.0.0.1/admin
And in sitemap.xml I see:

http://127.0.0.1/sitemap.xmlhttp://127.0.0.1/sitemap.xml
This is my navigation (xml) file:

<?xml version="1.0" encoding="UTF-8"?>
defaultdefaultdefaultadminadminadminadmin

_request objects in SitemapController are the same. I think this is bug.

Comments

Code tags added.

This not a bug! This is the normal behavior from Zend_Controller_Action_Helper "url".

Your configuration:


$urlHelper = Zend_Controller_Action_HelperBroker::getStaticHelper('Url');

Zend_Debug::dump($urlHelper->url(
    array(
        'module' => 'default',
    ),
    null,
    true
));
Zend_Debug::dump($urlHelper->url(
    array(
        'module' => 'admin',
    ),
    null,
    true
));

string(39) "/test/public/sitemap.xml"

string(39) "/test/public/sitemap.xml"

The correct configuration:


$urlHelper = Zend_Controller_Action_HelperBroker::getStaticHelper('Url');

Zend_Debug::dump($urlHelper->url(
    array(),
    'default', // Route name
    true
));
Zend_Debug::dump($urlHelper->url(
    array(),
    'admin', // Route name
    true
));

string(28) "/test/public/"

string(33) "/test/public/admin"

Your xml-file should look like this:


<?xml version="1.0" encoding="UTF-8"?>
default
            defaultdefaultadmin
            adminadminadmin

<?xml version="1.0" encoding="UTF-8"?>
www.sitemaps.org/schemas/sitemap/0.9">
    http://localhost/test/public/http://localhost/test/public/admin

I don't think that this explains everything. Your configuration needs routes had been created, and this is not mondatory in navigation.xml. But if sitemap/xml works fine, then why sitemap.xml (routing to sitemap/xml) generates wrong urls?

Hi Andrey, please have a look at ZF-11261 and my comment on it.

Thank you, Kai. But my problem not with route indeed. I think, that if I'm trying to create sitemap from the same container, I have to see equal results. Maybe sitemap helper have to use urlHelper::simple($action) method. I don't know.

{quote} I think, that if I'm trying to create sitemap from the same container, I have to see equal results. {quote} No problem: add the route names to your xml configuration. That's all.

  • If the request is "sitemap.xml" and no route is given, then the current active route is used. In this case your custom route "sitemap".
  • If the request is "sitemap/xml" and no route is given, then the actual route instance with the name 'default' is used.

{quote} Maybe sitemap helper have to use urlHelper::simple($action) method. {quote} Very bad idea, because:


/**
 * Create URL based on default route
 * …
 */
public function simple($action, $controller = null, $module = null, array $params = null)

The method "simple" never use your custom routes!

By the way, the creation of URLs is not the task of Zend_Navigation and their helpers. It is also not allowed to change the behavior of Zend_Controller_Router.

Greetings Kaiuwe

{quote} It is also not allowed to change the behavior of Zend_Controller_Router. {quote} Better: Zend_Navigation is not allowed to change the behavior.

{quote} No problem: add the route names to your xml configuration. That's all. {quote} When I'm adding route "admin" I see only one url in sitemap: default.

I found that url is generating in Zend_Controller_Router_Rewrite::assemble And if I'm requesting sitemap/xml route object is instance of Zend_Controller_Router_Route_Module

but if I'm requesting sitemap.xml route object is instance of Zend_Controller_Router_Route_Static

I don't understand exactly why sitemap helper depends on route, but not on navigation container data.

{quote} I don't understand exactly why sitemap helper depends on route, but not on navigation container data. {quote} The navigation container data is always used! Only for the creation of the URLs the route name is needed.

Please add to all your routes in the configuration file the a route name.

Should there be other problems, then write me an email. The bug tracker is not a discussion forum!

Sorry: add to all your pages in the configuration file the a route name.