Manu
Manu

Reputation: 4500

Creating a rss feed in Symfony

I'm trying to create a RSS feed in Symfony.

I've added the following route :

rss_every_content:
  url:    /rss/all
  param: { module: content, action: index, sf_format: rss }
  requirements:
    sf_method: [get]

And I created a file called indexSuccess.rss.php in module/content/templates/ :

test message

But when I go to the url mysite/rss, all I get is an empty page ! No content at all, not even the debug toolbar... Help ! What is going on ?

Upvotes: 0

Views: 4266

Answers (3)

Azeem Solangi
Azeem Solangi

Reputation: 1

Controller Method
//------------------------latest 10 RSS FEED----------------------------------
    public function generateRssFeedsAction()
    {
     $em = $this->getDoctrine()->getManager();
     $newsObj = $em->getRepository('CrossOverAppUserBundle:News')->findlatestNewArticle($page_number=1, $limit=10);
     $response = new Response($this->renderView('CrossOverAppUserBundle:News:rss_feed.xml.twig',array('news'=>$newsObj)));
     $response->headers->set('Content-Type', 'application/xml; charset=utf-8');
     return $response;
    }

Twig Templete 
{% autoescape %}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        {% for entry in news %}
            <article_id>{{ entry.id }}</article_id>
            <article_name>{{ entry.newsTitle }}</article_name>
            <description>{{ entry.newDescription }}</description>
            <article_createdby>{{ entry.user.firstName }}</article_createdby>
            <article_createdat>{{ entry.createdAt|date("d-m-Y H:i:s") }}</article_createdat>
             <article_image><img src="{{ asset(entry.WebPath) }}" width="50" height="50"/></article_image>

        {% endfor %}
    </url>
</urlset>
{% endautoescape %}

Upvotes: -1

OrganicPanda
OrganicPanda

Reputation: 2687

I had the same problem as you. I noticed that sf_format: xml with indexSuccess.xml.php seems to work as long as you specify the RSS XML tag at the top like:

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">

<channel>
    <title>RSS Example</title>
    <description>This is an example of an RSS feed</description>
    <link>http://www.domain.com/link.htm</link>
    <lastBuildDate>Mon, 28 Aug 2006 11:12:55 -0400 </lastBuildDate>
    <pubDate>Tue, 29 Aug 2006 09:00:00 -0400</pubDate>

    <item>
        <title>Item Example</title>
        <description>This is an example of an Item</description>
        <link>http://www.domain.com/link.htm</link>
        <guid isPermaLink="false">1102345</guid>
        <pubDate>Tue, 29 Aug 2006 09:00:00 -0400</pubDate>
    </item>

</channel>
</rss>

From http://www.rss-tools.com/rss-example.htm

It's a hack but I can't see how else to do it.

Upvotes: 3

Valentin
Valentin

Reputation: 31

By default, RSS format is not supported by a symfony application, but you can add these lines in your factories.yml:

all: 
  request:
    param:
      formats:
        rss: application/rss+xml

All request with $sf_format = rss will by responded with MimeType application/rss+xml

Upvotes: 3

Related Questions