Published by Fabian on 16 Jan 2008 at 09:51 am
Easy way of managing your application feeds
Creating feeds in symfony is already quite easy. If you want to create feeds you can have a look at the Askeet tutorial day 11 which describes how to create a feed with sfFeedPlugin. But If you can you should use sfFeed2Plugin which is the enhanced and maintained version. What you now need to do is to create your feeds.
I am using a simple routing rule pattern
feed_whatever:
url: /feed/:type/$whateverName/:extraInformation
param: { module: feed, action: $whateverAction, type: rss}I want to support both atom and rss feeds so this is handled for all routes in the feed pre execute:
public function preExecute() { $type = $this->getRequestParameter('type'); switch ($type) { case "atom": $this->feed = new sfAtom1Feed(); break; case "rss": $this->feed = new sfRssFeed(); break; } }
Doing so enables you $whateverAction to use $this->feed and generate a polymorphic feed, which will be returned as requested either as atom or rss.
The next step is to provide easy integration of those two. For that I created a feed Helper
function link_to_feed($name, $uri, $parameters = array(), $type = "rss"){ if ($type == "rss") { $feed = "rss"; $icon = "rss"; $name = $name." (RSS 2.0)"; } else { $feed = "atom"; $icon = "feed"; $name = $name." (ATOM 1.0)"; } $uri = "@".$uri."?type=".$feed; foreach($parameters as $key => $value){ $uri .= "&".$key."=".$value; } $options = array('alt' => $name, 'title' => $name, 'align' => 'absmiddle'); $sf_request = sfContext::getInstance()->getRequest(); $sf_request->setParameter('tvfeeds', $sf_request->getParameter('myfeeds'). auto_discovery_link_tag($feed,$uri,$options)."\n"); return link_to(image_tag('icons/'.$icon, $options), $uri); } function link_to_feeds($name, $uri, $parameters = array()){ return link_to_feed($name, $uri, $parameters, "rss")." ". link_to_feed($name, $uri, $parameters, "atom"); }
This easy helper will automatically create both icons and routes when invoked like this:
echo link_to_feeds("Newest Post","feed_posts",array("extraInformation"=>"user:15"));
Note the absence of the “@” in the route name, as the final route gets constructed inside.
One important part of magic gets done when setting that request parameter. You might know that symfony has a helper which helps you creating those “link alt” tags, called auto_discovery_link_tag, which the browser reads and can put a nice feed icon into the adress bar. However it needs to get to the header section.
To do so this is a slight hack. Dave Dash came up with another solution, which I was unaware of when implementing mine. I think both are simliar in idea, but different in doing, choose which you like best or invent your own
.
When using the helper I automatically put the html code of a auto_discovery_link_tag representing this normal link to the feed into a request parameter. It does not have a fancy namespace. just called myfeeds.
Then in the layout.php I just need to do:
echo $sf_request->getParameter('myfeeds');
and all prepared auto_discovery_link’s are printed there. it also does nicely nothing if there are no links inside this parameter.
If you like you can also make this a get_autlink_for_header() helper but I was to lazy doing so (unfortunatly having that myfeeds hardcoded in two files instead of just one
).
I hope I could give you some ideas, or help you out.
snk00sj on 18 Jan 2008 at 6:44 pm #
Very interesting read thanks…
Symfony.es » Blog Archive » Una semana con Symfony #28 (12-20 enero 2008) on 03 Feb 2008 at 1:08 pm #
[...] Easy way of managing your application feeds [...]