Professional Documents
Culture Documents
You have a working Laravel app but want to add an RSS feed. In this tutorial I go though my simple solution to implement rss feed
on any Laravel application.
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.phpfile
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 publicfolder
I am sure you understood none of the above instructions, so let me explain this in a bit more detail.
https://www.codeproject.com/Tips/1267602/Adding-RSS-feed-to-your-Laravel-Blog?display=Print 1/4
20/11/2018 Adding RSS feed to your Laravel Blog - CodeProject
@foreach($posts as $post)
<item>
<title><![CDATA[{!! $post->title !!}]]></title>
<link>{{ route('pages.post', $post->slug) }}</link>
<guid isPermaLink="true">{{ route('pages.post', $post->slug) }}</guid>
<description><![CDATA[{!! $post->description !!}]]></description>
<content:encoded><![CDATA[{!! $post->content !!}]]></content:encoded>
<dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">{{ $post->author }}</dc:creator>
<pubDate>{{ $post->created_at->format(DateTime::RSS) }}</pubDate>
</item>
@endforeach
</channel>
</rss>
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.
<?php
use Illuminate\Support\Facades\Storage;
use App\Models\Post;
Artisan::command('generate:feed', function () {
$this->info("Generating RSS Feed");
$site = [
'name' => 'YOUR SITE NAME', // Simplest Web
'url' => 'YOUR SITE URL', // Link to your rss.xml. eg. https://simplestweb.in/rss.xml
'description' => 'YOUR SITE DESCRIPTION',
'language' => 'YOUR SITE LANGUAGE', // eg. en, en-IN, jp
'lastBuildDate' => $posts[0]->created_at->format(DateTime::RSS), // This generates the
latest posts date in RSS compatible format
];
// Passes posts and site data into the rss.blade.view, out comes text in rss format
https://www.codeproject.com/Tips/1267602/Adding-RSS-feed-to-your-Laravel-Blog?display=Print 2/4
20/11/2018 Adding RSS feed to your Laravel Blog - CodeProject
$rssFileContents = view('rss', compact('posts', 'site'));
// Saves the generated rss feed into a file called rss.xml in the public folder, this works
only
Storage::disk('local')->put('rss.xml', $rssFileContents);
$this->info("Completed");
});
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?.
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
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
This member doesn't quite have enough reputation to be able to display their biography and homepage.
https://www.codeproject.com/Tips/1267602/Adding-RSS-feed-to-your-Laravel-Blog?display=Print 3/4
20/11/2018 Adding RSS feed to your Laravel Blog - CodeProject
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile Article Copyright 2018 by Adi Sk
Web04-2016 | 2.8.181119.1 | Last Updated 20 Nov 2018 Everything else Copyright © CodeProject, 1999-2018
https://www.codeproject.com/Tips/1267602/Adding-RSS-feed-to-your-Laravel-Blog?display=Print 4/4