Adding RSS feed to your Laravel Blog

in #laravel6 years ago

image.png

Hello Again,

As you might already know, I rebuilt my site with Laravel in a day. Though the website took shape within a day's work, I still had many essential features to include, RSS feeds, blog post comments, structured data to name a few. I wanted to explain my solution to implementing an RSS feed. This may be a simple solution but the point is that it works like charm. So let's get started building an RSS feed for your site.

TLDR;

Here's the gist of my solution.

  1. Create a view file in the resources folder, this file will have the xml structure which can be populated dynamically
  2. Create an Artisan command called generate:feed in the routes/console.php file
  3. This command fetches all the posts and sends them to the RSS view. Then writes the returned xml formatted data to a file named rss.xml in the public folder

I am sure you understood none of the above instructions, so let me explain this in a bit more detail.

The RSS view

Create a file in your resources/views folder, I called it rss.blade.php. Paste in the code shown below. I'll explain as we go.

~~~ embed:789d44a628109a2e89b5d75cb08f595b#file-console-php gist metadata:c2thZGltb29sYW0vNzg5ZDQ0YTYyODEwOWEyZTg5YjVkNzVjYjA4ZjU5NWIjZmlsZS1jb25zb2xlLXBocA== ~~~

As you might notice we pass two variables to this view, $site and $posts. First contains some information about the your site. The second one contains an array of posts you want to include in this feed.

Next, we will create the Artisan command that controls the logic.

The Artisan Command

You may ask why write all the logic in a command instead of a controller. I have a few reasons why I chose this method.

  1. I may want to call this command from the terminal
  2. I may also call it from a controller
  3. Since this command generates a file in the public folder there's no need for requests to go through Laravel.

Below is my code in the routes/console.php file.

~~~ embed:789d44a628109a2e89b5d75cb08f595b#file-rss-blade-php gist metadata:c2thZGltb29sYW0vNzg5ZDQ0YTYyODEwOWEyZTg5YjVkNzVjYjA4ZjU5NWIjZmlsZS1yc3MtYmxhZGUtcGhw ~~~

This is pretty basic stuff you should be able to recognize what the code does. First, we create an Artisan command. Then we fetch all the posts sorted by the most recent ones. Next, we create an array with our site details. Then we pass all this data to our rss.blade.php view file, which returns the formatted xml we need. Lastly, we write this to the rss.xml file in our public folder. That was not hard, was it?.

Updating the layout file

Add the below code to the head section of all your pages.

~~~ embed:789d44a628109a2e89b5d75cb08f595b#file-layout-blade-php gist metadata:c2thZGltb29sYW0vNzg5ZDQ0YTYyODEwOWEyZTg5YjVkNzVjYjA4ZjU5NWIjZmlsZS1sYXlvdXQtYmxhZGUtcGhw ~~~

The above code allows RSS readers know that there's an rss feed in this site and points it to the url.

That's about it

I hope you understood my solution to implementing a RSS Feed for a Laravel site. There a lot more information you can add to this feed, you can find more resources below. If you like this tutorial, do let me know. I will write more of them. If you get stuck anywhere in the tutorial, let me know, I'll help you.

https://validator.w3.org/feed/
https://www.w3schools.com/xml/xml_rss.asp

Thank You
More about me at simplestweb.in

Sort:  

Source
Plagiarism is the copying & pasting of others work without giving credit to the original author or artist. Plagiarized posts are considered spam.

Spam is discouraged by the community, and may result in action from the cheetah bot.

More information and tips on sharing content.

If you believe this comment is in error, please contact us in #disputes on Discord

Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
https://dzone.com/articles/adding-rss-feed-to-your-laravel-blog