ZF2 how to disable Zend\Feed\Writer\Feed extensions

限于喜欢 提交于 2019-12-13 00:21:57

问题


Trying to create a simple rss feed in zend framework2 by using Zend\Feed\Writer\Feed:

$feed = new \Zend\Feed\Writer\Feed();

...

$out = $feed->export('rss');
echo $out;

And this will output:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  <channel>
    <title>example</title>
    <description>example</description>
    <generator>Zend_Feed_Writer 2 (http://framework.zend.com)</generator>
    <link>http://www.google.com</link>
    <item>
      <title>article1</title>
      <pubDate>Fri, 11 Apr 2014 06:32:53 +0000</pubDate>
      <slash:comments>0</slash:comments>
    </item>
    <item>
      <title>article2</title>
      <pubDate>Fri, 11 Apr 2014 06:32:53 +0000</pubDate>
      <slash:comments>0</slash:comments>
    </item>
  </channel>
</rss>

My question is:

how to disable xmlns:slash extension <slash:comments>0</slash:comments>?


回答1:


It's quite a mess in the Zend\Feed component with extensions. I have been messing around with it for a while.

What happens?

You construct a Zend\Feed\Writer\Feed. The Feed extends the Zend\Feed\Writer\AbstractFeed. In the __construct() of AbstractFeed happens this:

public function __construct()
{
    Writer::registerCoreExtensions();
    $this->_loadExtensions();
}

Writer is here the Zend\Feed\Writer\Writer. The registerCoreExtensions looks as follows:

public static function registerCoreExtensions()
{
    static::registerExtension('DublinCore');
    static::registerExtension('Content');
    static::registerExtension('Atom');
    static::registerExtension('Slash');
    static::registerExtension('WellFormedWeb');
    static::registerExtension('Threading');
    static::registerExtension('ITunes');
}

Here, you see the different extens are added to the static Writer instance. From there on, the _loadExtensions() call fetches all extensions registered from the Writer and imports them into the Feed. The same happens by the way in the AbstractRenderer.

How to fix?

Because internally inside the Feed and Renderer the registerCoreExtensions() is called, you cannot overwrite the default list of extensions. Also, the Feed and the Renderer do not have getters/setters for the extension. The only way I came up with is to write your own Feed object and Renderer object.

For the Feed object you create a getter/setter to remove the extensions you want. You also override export where you copy the contents of the method, but overwrite the $renderClass variable name to the render class of your own.

For the Renderer class you create a getter/setter to remove the extensions you want as well. This way, you can tune the extensions you need. It's not that pretty, but hopefully it's something improved upon in ZF3 :)



来源:https://stackoverflow.com/questions/23005571/zf2-how-to-disable-zend-feed-writer-feed-extensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!