Skip to content

Add a blog posts RSS channel #425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/Resources/translations/messages.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@
<source>menu.logout</source>
<target>Logout</target>
</trans-unit>
<trans-unit id="menu.rss">
<source>menu.rss</source>
<target>Blog Posts RSS</target>
</trans-unit>

<trans-unit id="post.to_publish_a_comment">
<source>post.to_publish_a_comment</source>
Expand Down Expand Up @@ -317,6 +321,15 @@
<source>help.more_information</source>
<target><![CDATA[For more information, check out the <a href="http://symfony.com/doc">Symfony doc</a>.]]></target>
</trans-unit>

<trans-unit id="rss.title">
<source>rss.title</source>
<target>Symfony Demo blog</target>
</trans-unit>
<trans-unit id="rss.description">
<source>rss.description</source>
<target>Most recent posts published on the Symfony Demo blog</target>
</trans-unit>
</body>
</file>
</xliff>
1 change: 1 addition & 0 deletions app/Resources/views/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>{% block title %}Symfony Demo application{% endblock %}</title>
<link rel="alternate" type="application/rss+xml" title="{{ 'rss.title'|trans }}" href="{{ path('blog_rss') }}">
{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('css/bootstrap-flatly-3.3.7.min.css') }}">
<link rel="stylesheet" href="{{ asset('css/font-awesome-4.6.3.min.css') }}">
Expand Down
5 changes: 5 additions & 0 deletions app/Resources/views/blog/_rss.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="section rss">
<a href="{{ path('blog_rss') }}">
<i class="fa fa-rss" aria-hidden="true"></i> {{ 'menu.rss'|trans }}
</a>
</div>
1 change: 1 addition & 0 deletions app/Resources/views/blog/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@
{{ parent() }}

{{ show_source_code(_self) }}
{{ include('blog/_rss.html.twig') }}
{% endblock %}
22 changes: 22 additions & 0 deletions app/Resources/views/blog/index.xml.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>{{ 'rss.title'|trans }}</title>
<description>{{ 'rss.description'|trans }}</description>
<pubDate>{{ 'now'|date('r', timezone='GMT') }}</pubDate>
<lastBuildDate>{{ (posts|last).publishedAt|default('now')|date('r', timezone='GMT') }}</lastBuildDate>
<link>{{ url('blog_index') }}</link>
<language>{{ app.request.locale }}</language>

{% for post in posts %}
<item>
<title>{{ post.title }}</title>
<description>{{ post.summary }}</description>
<link>{{ url('blog_post', {'slug': post.slug}) }}</link>
<guid>{{ url('blog_post', {'slug': post.slug}) }}</guid>
<pubDate>{{ post.publishedAt|date(format='r', timezone='GMT') }}</pubDate>
<author>{{ post.authorEmail }}</author>
</item>
{% endfor %}
</channel>
</rss>
1 change: 1 addition & 0 deletions app/Resources/views/blog/post_show.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@
{{ parent() }}

{{ show_source_code(_self) }}
{{ include('blog/_rss.html.twig') }}
{% endblock %}
16 changes: 12 additions & 4 deletions src/AppBundle/Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,24 @@
class BlogController extends Controller
{
/**
* @Route("/", defaults={"page": "1"}, name="blog_index")
* @Route("/page/{page}", requirements={"page": "[1-9]\d*"}, name="blog_index_paginated")
* @Route("/", defaults={"page": "1", "_format"="html"}, name="blog_index")
* @Route("/rss.xml", defaults={"page": "1", "_format"="xml"}, name="blog_rss")
* @Route("/page/{page}", defaults={"_format"="html"}, requirements={"page": "[1-9]\d*"}, name="blog_index_paginated")
* @Method("GET")
* @Cache(smaxage="10")
*
* NOTE: For standard formats, Symfony will also automatically choose the best
* Content-Type header for the response.
* See http://symfony.com/doc/current/quick_tour/the_controller.html#using-formats
*/
public function indexAction($page)
public function indexAction($page, $_format)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still name this variable $format.

Copy link
Member Author

@yceruto yceruto Jan 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We lost then the auto Content-Type header response specially in blog_rss route, remember we need the special variable _format into defaults option to make it automatically.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, you're right.

{
$posts = $this->getDoctrine()->getRepository(Post::class)->findLatest($page);

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

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/AppBundle/Controller/BlogControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,21 @@ public function testIndex()
'The homepage displays the right number of posts.'
);
}

public function testRss()
{
$client = static::createClient();
$crawler = $client->request('GET', '/en/blog/rss.xml');

$this->assertSame(
'text/xml; charset=UTF-8',
$client->getResponse()->headers->get('Content-Type')
);

$this->assertCount(
Post::NUM_ITEMS,
$crawler->filter('item'),
'The xml file displays the right number of posts.'
);
}
}
7 changes: 7 additions & 0 deletions web/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ footer #footer-resources i {
min-width: 75px
}

/* Misc. elements
------------------------------------------------------------------------- */
.section.rss a {
color: #f39c12;
font-size: 21px;
}

/* Forms
------------------------------------------------------------------------- */
.form-group.has-error .form-control {
Expand Down