Posts Learn Snippets Categories Tags About
/
How to create RSS Feed in Laravel cover

How to create RSS Feed in Laravel

Learn how to create RSS Feed in your Laravel project to allow your user get up to date with your website content.

1 month ago

8 mins read

611 views

In this post, you'll be learning how to implement RSS Feed in Laravel. The steps are very simple so do follow along and you will have it implemented in no time. To achieve this we'll be using a Laravel Package by Laravelium called Laravel Feed.

Setup
To get started install the package into your application by running the code below.
composer require laravelium/feed

By default, it will take the latest version but if you are using the old version of Larave do explicitly specify it in your composer.json file. If you need to change the default views/template provide by the package do publish the view that's provided by the package.
php artisan vendor:publish --provider="Laravelium\Feed\FeedServiceProvider"

Implementation
To generate the RSS Feed, you will have to define a new route in web.php and just call it "rss-feed" which essentially is the URL user has to call to view the RSS feed.
Route::get('rss-feed', function () {
    /* create new feed instance */
    $feed = app("feed");
});

Once you have defined that you'll need to query the modal that you want to generate as RSS Feed. In this example let's assume "post" is the resource so let's query 20 posts but make sure it's already published.
$posts = Post::query()
    ->whereNotNull('published_at')
    ->take(20)
    ->get();

To create the main structure of the RSS feed we need to call some of the helper methods provided by the feed instance. It will roughly look like below. Do note that you ensure all of the data is accurate since it's very important to ensure that your subscriber are getting the right data.
/* set your feed's title, description, link, pubdate and language */
$feed->title = 'Your site name here';
$feed->description = 'Your site description here';
$feed->logo = 'https://your-site-url.test/img/logo.png';
$feed->link = 'https://your-site-url.test';
$feed->setDateFormat('datetime'); // 'datetime', 'timestamp' or 'carbon'
$feed->pubdate = $posts[0]->published_at;
$feed->lang = 'en';
$feed->setShortening(true); // true or false
$feed->setTextLimit(100); // maximum length of description text

Now since the main structure is already in place, to load the resource do loop it through and add it to the feed using "addItem" method. It will look like the following but do change it with your own data structure.
$posts->each(fn ($post) => $feed->addItem([
    'title' => $post->title,
    'author' => 'Your Name',
    'url' => $post->url,
    'link' => $post->url,
    'pubdate' => $post->published_at,
    'description' => $post->summary,
    'content' => $post->body
]));
 
Once everything is ready, now you can render the feed data by calling the render and specify the type which in this case is "rss". Do not that the "application/xml" is necessary to set the right document encoding for the browser to render.
$feed->ctype = "application/xml";

return $feed->render('rss');

Final Code
The final code will look like the following and once it's ready, try to view it on the browser by visiting "rss-feed" path.
Route::get('rss-feed', function () {
    /* create new feed */
    $feed = app("feed");

    $posts = Post::query()
        ->whereNotNull('published_at')
        ->take(20)
        ->get();

    /* set your feed's title, description, link, pubdate and language */
    $feed->title = 'Your site name here';
    $feed->description = 'Your site description here';
    $feed->logo = 'https://your-site-url.test/img/logo.png';
    $feed->link = 'https://your-site-url.test';
    $feed->setDateFormat('datetime'); /* 'datetime', 'timestamp' or 'carbon' */
    $feed->pubdate = $posts[0]->published_at;
    $feed->lang = 'en';
    $feed->setShortening(true); /* true or false */
    $feed->setTextLimit(100); /* maximum length of description text */

    $posts->each(fn ($post) => $feed->addItem([
        'title' => $post->title,
        'author' => 'Your Name',
        'url' => $post->url,
        'link' => $post->url,
        'pubdate' => $post->published_at,
        'description' => $post->summary,
        'content' => $post->body
    ]));

    $feed->ctype = "application/xml";

    return $feed->render('rss');
});

I hope this tutorial is helpful for you and if there's any question, do comment out below and let's start the discussion. Also, don't forget to share it with your friend as well and I would appreciate it a lot. Cheers and have a good try.