DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Integrating Redis With Message Brokers
  • Designing a Blog Application Using Document Databases
  • Terraform State File: Key Challenges and Solutions
  • How to Reorganize and Rebuild Indexes in MS SQL?

Trending

  • Google Cloud Document AI Basics
  • Top Book Picks for Site Reliability Engineers
  • Integrating Security as Code: A Necessity for DevSecOps
  • Building Scalable and Resilient Data Pipelines With Apache Airflow
  1. DZone
  2. Coding
  3. Frameworks
  4. Adding an RSS Feed to Your Blog With Laravel

Adding an RSS Feed to Your Blog With Laravel

All you need is PHP, Laravel, XML, and dash of HTML, and in about 40 lines of code you'll have your own RSS feed!

By 
Adi Sk user avatar
Adi Sk
·
Dec. 03, 18 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
22.8K Views

Join the DZone community and get the full member experience.

Join For Free

Hello again!

In another article, I decribed how 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, such as RSS feeds, blog post comments, and structured data, to name a few. I wanted to explain my solution for implementing an RSS feed. This may be a simple solution but the point is that it works like a 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 theresources 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.

{!! '<'.'?'.'xml version="1.0" encoding="UTF-8" ?>' !!}
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:media="http://search.yahoo.com/mrss/">
  <channel>
    <title>{{ $site['name'] }}</title>
    <link>{{ $site['url'] }}</link>
    <description><![CDATA[{{ $site['description'] }}]]></description>
    <atom:link href="{{ $site['url'] }}" rel="self" type="application/rss+xml" />
    <language>{{ $site['language'] }}</language>
    <lastBuildDate>{{ $site['lastBuildDate'] }}</lastBuildDate>

    @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. The first one 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.

The Artisan Command

You may ask why we need to 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.

<?php
use Illuminate\Support\Facades\Storage;
use App\Models\Post;
Artisan::command('generate:feed', function () {
  $this->info("Generating RSS Feed");
  // It is important that you sort by the latest post
  $posts = Post::where('published', true)->latest()->get();

  $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
  $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, so 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't too hard, was it?

Updating the Layout File

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

<!-- Change the title and href to your site -->
<link rel="alternate" type="application/rss+xml" title="Simplest Web" href="https://simplestweb.in/rss.xml" />

The above code lets the 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's a lot more information you can add to this feed, and 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'm happy to help. Thanks!

Additional Resources

  1. https://validator.w3.org

  2. /https://www.w3schools.com/xml/xml_rss.asp

Laravel Blog Command (computing)

Published at DZone with permission of Adi Sk. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Integrating Redis With Message Brokers
  • Designing a Blog Application Using Document Databases
  • Terraform State File: Key Challenges and Solutions
  • How to Reorganize and Rebuild Indexes in MS SQL?

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!