Skip to content

Commit 8a51859

Browse files
committed
add rss channel
1 parent 21f82cc commit 8a51859

File tree

5 files changed

+71
-4
lines changed

5 files changed

+71
-4
lines changed

app/Resources/translations/messages.en.xlf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@
243243
<source>menu.logout</source>
244244
<target>Logout</target>
245245
</trans-unit>
246+
<trans-unit id="menu.rss">
247+
<source>menu.rss</source>
248+
<target>Blog Posts RSS</target>
249+
</trans-unit>
246250

247251
<trans-unit id="post.to_publish_a_comment">
248252
<source>post.to_publish_a_comment</source>
@@ -317,6 +321,15 @@
317321
<source>help.more_information</source>
318322
<target><![CDATA[For more information, check out the <a href="http://symfony.com/doc">Symfony doc</a>.]]></target>
319323
</trans-unit>
324+
325+
<trans-unit id="rss.title">
326+
<source>rss.title</source>
327+
<target>Symfony Demo Blog</target>
328+
</trans-unit>
329+
<trans-unit id="rss.description">
330+
<source>rss.description</source>
331+
<target>Most recent posts published on the Symfony Demo project blog</target>
332+
</trans-unit>
320333
</body>
321334
</file>
322335
</xliff>

app/Resources/views/base.html.twig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<meta charset="UTF-8" />
1010
<meta name="viewport" content="width=device-width, initial-scale=1"/>
1111
<title>{% block title %}Symfony Demo application{% endblock %}</title>
12+
<link rel="alternate" type="application/rss+xml" href="{{ path('blog_rss') }}">
1213
{% block stylesheets %}
1314
<link rel="stylesheet" href="{{ asset('css/bootstrap-flatly-3.3.7.min.css') }}">
1415
<link rel="stylesheet" href="{{ asset('css/font-awesome-4.6.3.min.css') }}">
@@ -44,6 +45,12 @@
4445
<ul class="nav navbar-nav navbar-right">
4546

4647
{% block header_navigation_links %}
48+
<li>
49+
<a href="{{ path('blog_rss') }}">
50+
<i class="fa fa-rss" aria-hidden="true"></i> {{ 'menu.rss'|trans }}
51+
</a>
52+
</li>
53+
4754
<li>
4855
<a href="{{ path('blog_index') }}">
4956
<i class="fa fa-home" aria-hidden="true"></i> {{ 'menu.homepage'|trans }}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<rss version="2.0">
3+
<channel>
4+
<title>{{ 'rss.title'|trans }}</title>
5+
<description>{{ 'rss.description'|trans }}</description>
6+
<pubDate>{{ 'now'|date('r', timezone='GMT') }}</pubDate>
7+
<lastBuildDate>{{ (posts|last).publishedAt|default('now')|date('r', timezone='GMT') }}</lastBuildDate>
8+
<link>{{ url('blog_index') }}</link>
9+
<language>{{ app.request.locale }}</language>
10+
11+
{% for post in posts %}
12+
<item>
13+
<title>{{ post.title }}</title>
14+
<description>{{ post.summary }}</description>
15+
<link>{{ url('blog_post', {'slug': post.slug}) }}</link>
16+
<guid>{{ url('blog_post', {'slug': post.slug}) }}</guid>
17+
<pubDate>{{ post.publishedAt|date(format='r', timezone='GMT') }}</pubDate>
18+
<author>{{ post.authorEmail }}</author>
19+
</item>
20+
{% endfor %}
21+
</channel>
22+
</rss>

src/AppBundle/Controller/BlogController.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,24 @@
3434
class BlogController extends Controller
3535
{
3636
/**
37-
* @Route("/", defaults={"page": "1"}, name="blog_index")
38-
* @Route("/page/{page}", requirements={"page": "[1-9]\d*"}, name="blog_index_paginated")
37+
* @Route("/", defaults={"page": "1", "_format"="html"}, name="blog_index")
38+
* @Route("/rss.{_format}", name="blog_rss", defaults={"page": "1", "_format"="xml"}, requirements={"_format": "xml"})
39+
* @Route("/page/{page}", defaults={"_format"="html"}, requirements={"page": "[1-9]\d*"}, name="blog_index_paginated")
3940
* @Method("GET")
4041
* @Cache(smaxage="10")
42+
*
43+
* NOTE: For standard formats, Symfony will also automatically choose the best
44+
* Content-Type header for the response.
45+
* See http://symfony.com/doc/current/quick_tour/the_controller.html#using-formats
4146
*/
42-
public function indexAction($page)
47+
public function indexAction($page, $_format)
4348
{
4449
$posts = $this->getDoctrine()->getRepository(Post::class)->findLatest($page);
4550

46-
return $this->render('blog/index.html.twig', ['posts' => $posts]);
51+
// Every template name also has two extensions that specify the format and
52+
// engine for that template.
53+
// See https://symfony.com/doc/current/templating.html#template-suffix
54+
return $this->render('blog/index.'.$_format.'.twig', ['posts' => $posts]);
4755
}
4856

4957
/**

tests/AppBundle/Controller/BlogControllerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,21 @@ public function testIndex()
3737
'The homepage displays the right number of posts.'
3838
);
3939
}
40+
41+
public function testRss()
42+
{
43+
$client = static::createClient();
44+
$crawler = $client->request('GET', '/en/blog/rss.xml');
45+
46+
$this->assertSame(
47+
'text/xml; charset=UTF-8',
48+
$client->getResponse()->headers->get('Content-Type')
49+
);
50+
51+
$this->assertCount(
52+
Post::NUM_ITEMS,
53+
$crawler->filter('item'),
54+
'The xml file displays the right number of posts.'
55+
);
56+
}
4057
}

0 commit comments

Comments
 (0)