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

Add a blog posts RSS channel #425

merged 6 commits into from
Jan 12, 2017

Conversation

yceruto
Copy link
Member

@yceruto yceruto commented Jan 9, 2017

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! ;)

Copy link
Member

@javiereguiluz javiereguiluz left a comment

Choose a reason for hiding this comment

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

@yceruto thanks for implementing this feature. I like most of it ... but I've suggested some minor changes.

// This is a ResponseHeaderBag that has some nice methods for getting and
// setting response headers. The header names are normalized so that using
// Content-Type is equivalent to content-type or even content_type.
$response->headers->set('Content-Type', 'application/xhtml+xml');
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about this. It's great to show how this works in Symfony ... but it may look "horrible" to newcomers because they might think they always need to do that (but it's optional, because $this->render() takes care of that).

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 Simplified by using _format.

@@ -0,0 +1,23 @@
{% spaceless %}
Copy link
Member

Choose a reason for hiding this comment

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

Just asking: why this tag?

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.

Just for remove spaces between tags, but this detail can be ignored for simplicity
👍 Removed!

{% spaceless %}
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
Copy link
Member

Choose a reason for hiding this comment

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

What if we add these two tags to the <channel> tag?

<pubDate>{{ 'now'|date('r') }}</pubDate>
<lastBuildDate>{{ (posts|last).publishedAt|default('now')|date('r') }}</lastBuildDate>
  • pubDate: The original publication date for the channel or item. (optional)
  • lastBuildDate: The most recent time the content of the channel was modified. (optional)

They are totally optional, so if you don't agree that's OK!

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 Added!

<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=RSS, timezone='GMT') }}</pubDate>
Copy link
Member

Choose a reason for hiding this comment

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

Here I also have doubts about:

{% set RSS = constant('RSS', date()) %}
<pubDate>{{ post.publishedAt|date(format=RSS, timezone='GMT') }}</pubDate>

Why not using this?

<pubDate>{{ post.publishedAt|date('r', timezone='GMT') }}</pubDate>

Showing the Twig constants and all it's great ... but we should be careful. If newcomers see this, they'll think that Symfony is this complicated and you must do it like this.

Copy link
Member Author

Choose a reason for hiding this comment

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

You are right, RFC822 (r) and RSS constants have the same format. Fixed!

@@ -45,6 +45,12 @@

{% block header_navigation_links %}
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should also add the following to the <head> part of the page:

<link rel="alternate" type="application/rss+xml" href="{{ path('blog_rss') }}">

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 Added!

* @Route("/rss.xml", name="blog_rss")
* @Method("GET")
*/
public function rssAction()
Copy link
Contributor

Choose a reason for hiding this comment

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

@yceruto, I think it's not needed to create a particular action. You could use the _format parameter in the indexAction. See http://symfony.com/doc/current/quick_tour/the_controller.html#using-formats

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 Updated!

@yceruto
Copy link
Member Author

yceruto commented Jan 10, 2017

@javiereguiluz, @voronkovich Thanks for the review. I have addressed your comments.

* @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.{_format}", name="blog_rss", defaults={"page": "1", "_format"="xml"}, requirements={"_format": "xml"})
Copy link
Member

Choose a reason for hiding this comment

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

If we decide to have a different @Route for the RSS, maybe we can hardcode some things to simplify:

@Route("/rss.xml", name="blog_rss", defaults={"page": "1", "_format"="xml"})

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.

Totally agree! Fixed.

@@ -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" href="{{ path('blog_rss') }}">
Copy link
Member Author

Choose a reason for hiding this comment

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

The Content-Type that we return here is text/xml (by default), is it a contradiction? should change this type to text/xml then?

Copy link
Member

Choose a reason for hiding this comment

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

To be honest I don't know. It makes sense to only use text/xml ... but then, will browsers recognize that as a RSS link?

Copy link
Member Author

Choose a reason for hiding this comment

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

According to http://www.rssboard.org/rss-autodiscovery#element-link-type

The type attribute MUST contain the feed's MIME type, which is "application/rss+xml" for RSS 1.0 or RSS 2.0 feeds.

Copy link
Member

@javiereguiluz javiereguiluz left a comment

Choose a reason for hiding this comment

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

👍

@yceruto thanks for implementing this feature and for all the changes added during the review!

* @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", name="blog_rss", defaults={"page": "1", "_format"="xml"})
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we keep the order of arguments, i.e. move name="blog_rss" to the end like in others? 😇

*/
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.

@javiereguiluz
Copy link
Member

@yceruto thanks to the help of @iltar and @fbourigault in the Symfony Slack channel, I've managed to add a commit to your PR. The change I propose is to remove the RSS link from the header, which makes it look crowded:

before


And instead display it in the sidebar of blog/index and blog/show:

after

Copy link
Member

@javiereguiluz javiereguiluz left a comment

Choose a reason for hiding this comment

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

👍

@javiereguiluz
Copy link
Member

@yceruto thanks for implementing this feature. I truly love it. Thanks!

@javiereguiluz javiereguiluz merged commit d7420d5 into symfony:master Jan 12, 2017
javiereguiluz added a commit that referenced this pull request Jan 12, 2017
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
@yceruto yceruto deleted the rss_channel branch January 12, 2017 17:15
@javiereguiluz javiereguiluz mentioned this pull request Jan 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants