INDEX
Scope
This article is intended for use with Kohana 2.x version. I have tested only with the latest, which is 2.3.4 as of today.
Kohana’s « Feed » helper
The official documentation of the helper is located here :https://docs.kohanaphp.com/helpers/feed
With this helper you can easily create an RSS feed, however the method doesn’t natively support nested tags. So … won’t work and you’ll get this error :
SimpleXMLElement::addChild() expects parameter 2 to be string, array given
Workaround : syntax in your controller
Logic tends to nest your tags into a sub-array, like this :
public function rss() { $info = array('title' => 'Christian Lapie', 'notes' => 'Latest updates'); $items = ORM::factory('article') ->where('statut', 'publie') ->orderby('date', 'desc') ->find_all(); $rssitems = array(); foreach($items as $item) { $rssitems[] = array( 'title' => $item->titre, 'link' => Kohana::config('site.url_site').'/oeuvres/'.$item->id.'/'.url::title($item->titre), 'description' => $item->soustitre, 'author' => 'Christian Lapie', 'pubDate' => date("D, d M Y H:i:s +0100", strtotime($item->date)), 'image' => array( 'url' => Kohana::config('site.url_site').'/IMG/arton'.$item->id.'.jpg', 'title' => $item->titre, 'link' => Kohana::config('site.url_site').'/oeuvres/'.$item->id.'/'.url::title($item->titre) ) ); } echo feed::create($info, $rssitems); exit(); }
(look carefully at the image key and its sub-pairs)
Editing Kohana’s core
It turns out to be pretty simple.
You will open the following file :
/system/helpers/feed.php
Then find the following lines (around 114) :
// Add the info to the row $row->addChild($name, $value);
Finally replace the block with this :
// Add the info to the row if (is_array($value)) { $temp = $row->addChild($name); foreach($value as $key1 => $value1) { $temp->addChild($key1, $value1); } } else { $row->addChild($name, $value); }
Et voilà !
Now you can have a nicely valid tag :
Result
<item> <title>Les secrets d'un temps immense</title> <link>https://lapie.localhost/oeuvres/318/les-secrets-dun-temps-immense</link> <description>Varsovie, Blonie, Pologne (Poland)</description> <author>Christian Lapie</author> <pubDate>Wed, 12 Jan 2011 00:00:00 +0100</pubDate> <image> <url>https://lapie.localhost/IMG/arton318.jpg</url> <title>Les secrets d'un temps immense</title> <link>https://lapie.localhost/oeuvres/318/les-secrets-dun-temps-immense</link> </image> </item>
You can add optional parameters such as Width, Height… see the RSS Specifications here.
Please leave comment if you have found another (official) solution !
Germain
Great post. Exactly solved my problem.
In my case the file that need to be revised is located here:
SYSPATH/classes/Kohana/Feed.php [ 163 ]
Thanks for sharing.
Glad it helped!