2010-11-23 87 views
0

圖片站點地圖我生成使用Zend_Navigation一個網站地圖,我想圖像添加到該地圖,現在我還是不知道如何完成這件事,我用下面的(工作)代碼來生成網站地圖與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)); 

有誰知道我想要什麼,甚至有可能?我找不到在文檔或網頁什麼...

感謝您的時間, 多米尼克

回答

0

衝電氣所以首先你需要做的是延長Zend_Navigation_Page_Uri,並添加你的圖像VAR它SMTH像下面什麼:

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

將此類添加到庫/ Mylib/NavPageUriImage.php。

,使其可用,你需要註冊的名稱空間(我喜歡在引導登記自己的命名空間,但它可以從app.ini也做),這樣在你引導類添加以下內容:

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

以下是不推薦,您需要擴展YOU'RE OWN導航幫助,並使用(我只是沒有玩的時間現在用它)ALLSO加你自己imageValidator

然後在庫/ Zend的/視圖/助手/導航/ 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); 
}