使用 Zend_Navigation 进行图像站点地图

发布于 2024-10-03 18:06:12 字数 1389 浏览 3 评论 0原文

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

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>
         ....

我需要以下输出 网址部分

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

我尝试设置的

$newSite->image = URI

,但它不起作用,我也尝试通过添加自定义属性

$newSite->__set('image', array('loc' => URI));

有人知道我想要的是否可能吗?我在文档或网络中找不到任何内容...

感谢您的宝贵时间, 多米尼克

I generate a Sitemap using Zend_Navigation and i wanted to add images to this sitemap, now i got no Idea how to get this done, i use following (working) code to generate the 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);

The Output is as follows:

<?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>
         ....

I need the following Output inside the Url part

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

i tried to just set

$newSite->image = URI

but it didnt work also i tried to add custom attribute via

$newSite->__set('image', array('loc' => URI));

Does anyone know if what i want is even possible ? i cant find anything in the docs or web...

Thanks for your time,
Dominik

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年九夏 2024-10-10 18:06:12

好的,首先您需要做的是扩展 Zend_Navigation_Page_Uri 并将您的图像变量添加到其中,如下所示:

    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';

不推荐以下内容,您需要扩展您自己的导航助手并使用它(我现在没有时间玩它)全部添加您自己的 imageValidator

然后在 Library/zend/ 中view/helper/navigation/sitemap.php 添加以下行(在添加优先级元素 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);
}

Oki so first what you need to do is extend Zend_Navigation_Page_Uri and add you're image var to it smth like below :

    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()
            ));
    }
}

Add this class to library/Mylib/NavPageUriImage.php.

To make it usable you need to register the namespace ( i like to register my namespaces at bootstrap but it can be done from app.ini too ) so in you're bootstrap class add the following :

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

Then in you're controller you can now use :

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

THE FOLLOWING IS NOT RECOMENDED , YOU NEED TO EXTEND YOU'RE OWN NAVIGATION HELPER AND USE THAT ( i just don't have the time to play with it now ) ALLSO ADD YOU'RE OWN imageValidator

And then in library/zend/view/helper/navigation/sitemap.php add the following lines ( under the add priority element if statement , mine ends at 443 so i added this at 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);
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文