使用Zend_Navigation的图像站点地图

时间:2010-11-23 08:12:52

标签: image zend-framework sitemap

我使用Zend_Navigation生成Sitemap,我想将图像添加到此站点地图,现在我不知道如何完成此操作,我使用以下(工作)代码生成站点地图

foreach($sitemapItems as $item)
    {
        $newSite = new Zend_Navigation_Page_Uri();
        $newSite->uri = 'http://' . $_SERVER['HTTP_HOST'] . $item->getSpeakingUrl();
        $newSite->lastmod = $item->getUpdatedAt();
        $newSite->changefreq = 'weekly';

        $this->_navigation->addPage($newSite);
    }

    $this->view->navigation($this->_navigation);
    $this->view->navigation()->sitemap()->setFormatOutput(true);

输出如下:

<?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
        <url>
            <loc>http://test.dev/pictures/site-28.html</loc>
            <lastmod>2010-03-11T17:47:30+01:00</lastmod>
            <changefreq>weekly</changefreq>
         </url>
         ....

我需要在Url部分中使用以下输出

<image:image>
    <image:loc>http://example.com/image.jpg</image:loc>
</image:image> 

我试着设置

$newSite->image = URI

但它也没有用,我试图通过

添加自定义属性
$newSite->__set('image', array('loc' => URI));

有谁知道我想要的是否可能?我无法在文档或网页上找到任何内容......

谢谢你的时间, 多米尼克

1 个答案:

答案 0 :(得分:0)

Oki所以首先你需要做的是扩展Zend_Navigation_Page_Uri并将你的图像var添加到它如下所示:

    class Mylib_NavPageUriImage extends Zend_Navigation_Page_Uri
{
    protected $_image = null;

    public function setImage($image)
    {
        if (null !== $image && !is_string($image)) {
            require_once 'Zend/Navigation/Exception.php';
            throw new Zend_Navigation_Exception(
                    'Invalid argument: $image must be a string or null');
        }

        $this->_image = $image;
        return $this;
    }

    public function getImage()
    {
        return $this->_image;
    }

    public function toArray()
    {
        return array_merge(
            parent::toArray(),
            array(
                'image' => $this->getImage()
            ));
    }
}

将此类添加到library / Mylib / NavPageUriImage.php。

为了使它可用,你需要注册命名空间(我喜欢在bootstrap注册我的命名空间,但也可以从app.ini完成)所以在你的bootstrap类中添加以下内容:

function _initNamespace()
    {
        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->registerNamespace('Mylib_');
    }

然后在你的控制器中你现在可以使用:

$newSite = new Mylib_NavPageUriImage();
$newSite->uri = 'http://' . $_SERVER['HTTP_HOST'] . $item->getSpeakingUrl();
$newSite->lastmod = $item->getUpdatedAt();
$newSite->changefreq = 'weekly';
$newSite->image = 'some image';

以下内容未被推荐,您需要扩展您自己的导航并使用它(我现在没有时间玩它)所有人都添加了你自己的图像处理器

然后在library / zend / view / helper / navigation / sitemap.php中添加以下行(在add priority元素if语句下,我的结尾为443,所以我在444添加了这一行):

// add 'image' element if a valid image is set in page
if (isset($page->image)) {
    $image = $page->image;
        $imgDom = $dom->createElementNS(self::SITEMAP_NS, 'image:image');
        $imgDom->appendChild($dom->createElementNS(self::SITEMAP_NS, 'image:loc', $image));
    $urlNode->appendChild($imgDom);
}