Creating a rss feed in Symfony

人盡茶涼 提交于 2019-12-04 21:33:24
OrganicPanda

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.

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

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