Skip to content

Commit 4d93cda

Browse files
committed
feature #425 Add a blog posts RSS channel (yceruto, javiereguiluz)
This PR was merged into the master branch. Discussion ---------- Add a blog posts RSS channel This try to implement a simple RSS channel accepted in #155 since Aug 2015. Knowledge that brings: * Custom XML extension to specify the format of the template (`index.xml.twig`). * How to render a simple XML file. Any suggestions would be welcome! ;) Commits ------- d7420d5 Moved the RSS link to the sidebar cc8fe6b Minor tweaks 8a02a15 add rss title to link element 4e2c7db simplify the blog_rss route definition 254632c Minor tweaks in the translation strings 8a51859 add rss channel
2 parents c700a55 + d7420d5 commit 4d93cda

File tree

9 files changed

+79
-4
lines changed

9 files changed

+79
-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 blog</target>
332+
</trans-unit>
320333
</body>
321334
</file>
322335
</xliff>

app/Resources/views/base.html.twig

Lines changed: 1 addition & 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" title="{{ 'rss.title'|trans }}" 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') }}">
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div class="section rss">
2+
<a href="{{ path('blog_rss') }}">
3+
<i class="fa fa-rss" aria-hidden="true"></i> {{ 'menu.rss'|trans }}
4+
</a>
5+
</div>

app/Resources/views/blog/index.html.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@
2828
{{ parent() }}
2929

3030
{{ show_source_code(_self) }}
31+
{{ include('blog/_rss.html.twig') }}
3132
{% endblock %}
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>

app/Resources/views/blog/post_show.html.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,5 @@
6363
{{ parent() }}
6464

6565
{{ show_source_code(_self) }}
66+
{{ include('blog/_rss.html.twig') }}
6667
{% endblock %}

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.xml", defaults={"page": "1", "_format"="xml"}, name="blog_rss")
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
}

web/css/main.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ footer #footer-resources i {
118118
min-width: 75px
119119
}
120120

121+
/* Misc. elements
122+
------------------------------------------------------------------------- */
123+
.section.rss a {
124+
color: #f39c12;
125+
font-size: 21px;
126+
}
127+
121128
/* Forms
122129
------------------------------------------------------------------------- */
123130
.form-group.has-error .form-control {

0 commit comments

Comments
 (0)