Sling Academy
Home/PHP/How to create an RSS feed in Laravel

How to create an RSS feed in Laravel

Last updated: January 16, 2024

Introduction

Creating an RSS feed for your Laravel application allows users to stay updated with the latest content without needing to constantly check your website for updates. RSS (Really Simple Syndication) is a type of web feed that enables users to access updates to online content in a standardized, computer-readable format. In this guide, we’ll go through the steps to integrate an RSS feed into a Laravel application.

Initial Setup

Ensure that you have a Laravel project set up. If you don’t, you can create a new one by running the following Composer command:

composer create-project --prefer-dist laravel/laravel your-project-name

Once your project is ready, navigate into the project directory:

cd your-project-name

Next, let’s add a route for our RSS feed in the web.php file located in the routes directory.

Route::get('/feed', 'FeedController@feed')->name('feed');

Creating the Controller

Let’s now create a controller called FeedController:

php artisan make:controller FeedController

Open the newly created FeedController.php within the app/Http/Controllers directory and prepare to write the logic for generating the feed.

Generating the RSS Feed

In order to generate the RSS feed, we will use the Response class to create an XML format output. Add the following method to FeedController.php:

use Illuminate\Http\Response;

public function feed() {
  // Fetch latest content from your model, e.g. Post
  $posts = Post::latest()->get();

  $feedContent = "
<rss version='2.0'>
<channel>
    <title>Your Site Name</title>
    <link>https://your-site.com</link>
    <description>This is my RSS Feed</description>
    <language>en-us</language>";

  // Loop over the content and add it to the feed
  foreach ($posts as $post) {
    $feedContent .= "
    <item>
        <title>{$post->title}</title>
        <description>{$post->description}</description>
        <link>https://your-site.com/post/{$post->id}</link>
        <pubDate>{$post->created_at->toRssString()}</pubDate>
    </item>";
  }

  $feedContent .= "
</channel>
</rss>";

  return response($feedContent, 200)->header('Content-Type', 'application/rss+xml');
}

This code snippets retrieve the latest posts from the Post model, then constructs a string in the format of an RSS feed.

Cache Your Feed

It’s important to remember that generating your feed on each request might not be the most performance-efficient way. Consider caching your RSS feed output:

public function feed() {
  $cachedFeed = Cache::remember('rss-feed', 60, function () {
      // Your feed generation logic goes here
  });

  return response($cachedFeed, 200)->header('Content-Type', 'application/rss+xml');
}

Testing the Feed

You can test if the feed works by navigating to the /feed URL of your Laravel application in your web browser. You should see your RSS feed in XML format.

Customizing Your Feed

You should customize your RSS feed according to your needs, including adding image URLs or other metadata that compliantly follows the RSS standard.

Conclusion

The integration of an RSS feed into a Laravel application provides an efficient way for users to receive updates and engage with the content. Through Laravel, we can easily create and customize an RSS Feed for most types of content-driven applications.

Next Article: How to return stream response in Laravel (for large files)

Previous Article: How to create an XML sitemap in Laravel

Series: Laravel & Eloquent Tutorials

PHP

You May Also Like

  • Pandas DataFrame.value_counts() method: Explained with examples
  • Constructor Property Promotion in PHP: Tutorial & Examples
  • Understanding mixed types in PHP (5 examples)
  • Union Types in PHP: A practical guide (5 examples)
  • PHP: How to implement type checking in a function (PHP 8+)
  • Symfony + Doctrine: Implementing cursor-based pagination
  • Laravel + Eloquent: How to Group Data by Multiple Columns
  • PHP: How to convert CSV data to HTML tables
  • Using ‘never’ return type in PHP (PHP 8.1+)
  • Nullable (Optional) Types in PHP: A practical guide (5 examples)
  • Explore Attributes (Annotations) in Modern PHP (5 examples)
  • An introduction to WeakMap in PHP (6 examples)
  • Type Declarations for Class Properties in PHP (5 examples)
  • Static Return Type in PHP: Explained with examples
  • PHP: Using DocBlock comments to annotate variables
  • PHP: How to ping a server/website and get the response time
  • PHP: 3 Ways to Get City/Country from IP Address
  • PHP: How to find the mode(s) of an array (4 examples)
  • PHP: Calculate standard deviation & variance of an array