RSS Feed Example in Laravel Without Packages

Hello Artisans, today we'll discuss how to we can create RSS Feed on our website. RSS Feed is very much common feature for a daily content updating website like blogs or any news site. So in this RSS Feed example tutorial, I will explain that how we can create RSS feed in a very simple without using any third-party packages for our web application.

To create this RSS feed for the Laravel application and get data from RSS feed Laravel, I will use the User model to fetch data. You can use any model according to your RSS feed requirement. So let's get started..

Table of Contents

  1. Create Controller
  2. Define Route
  3. Create and Setup Blade File
  4. Output

Create Controller

First of all, we will create our controller to get data for generating RSS Feed. For creating RSS Feed we will need XML data.

Now create RssFeedController and update controller like this:

app/Http/Controllers/RssFeedController.php
<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class RssFeedController extends Controller
{
    public function generateRssFeed()
    {
        $users = User::orderByDesc('id')->get();

        return response()->view('feed', compact('users'))->header('Content-Type', 'application/xml');

    }
}

Define Route

Now, we will define our route

routes/web.php
............

Route::get('feed',[\App\Http\Controllers\RssFeedController::class,'generateRssFeed'])->name('rss.feed');
............

Create and Setup Blade File

now we will create and setup out blade file feed.blade.php like below:

routes/web.php
<?=
/* Using an echo tag here so the `<? ... ?>` won't get parsed as short tags */
'<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL
?>
<rss version="2.0" xmlns:media="http://www.w3.org/2001/XMLSchema" xml:base="{{ url('/feed') }}" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title><![CDATA[ shouts.dev ]]></title>
        <link>{{ url('/feed') }}</link>
        <description><![CDATA[ Laravel RSS Feed Example - shouts.dev! ]]></description>

        <language>en</language>
        <pubDate>{{ now()->toDayDateTimeString('Asia/Dhaka') }}</pubDate>

        @foreach($users as $user)
            <item>
                <title><![CDATA[{{ $user->name }}]]></title>
                <link>{{ $user->id }}</link>
                <description><![CDATA[{!! $user->email !!}]]></description>
                <category>{{ $user->createt_at }}</category>
                <author><![CDATA[{{ $user->name  }}]]></author>
                <guid>{{ $user->id }}</guid>
                <pubDate>{{ $user->created_at->toRssString() }}</pubDate>
            </item>
        @endforeach
    </channel>
</rss>

Output

Now just visit http://127.0.0.1:8000/feed to justify our content RSS Feed working or not. And the output will be something like:

routes/web.php
<rss xmlns:media="http://www.w3.org/2001/XMLSchema" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"
  xml:base="http://127.0.0.1:8000/feed">
  <channel>
    <title>
      <![CDATA[ shouts.dev ]]>
    </title>
    <link>http://127.0.0.1:8000/feed</link>
    <description>
      <![CDATA[ Laravel RSS Feed Example - shouts.dev! ]]>
    </description>
    <language>en</language>
    <pubDate>Sat, Jul 2, 2022 9:40 AM</pubDate>
    <item>
      <title>
        <![CDATA[ Test Person ]]>
      </title>
      <link>3</link>
      <description>
        <![CDATA[ [email protected] ]]>
      </description>
      <category />
      <author>
        <![CDATA[ Test Person ]]>
      </author>
      <guid>3</guid>
      <pubDate>Wed, 22 Jun 2022 20:20:14 +0000</pubDate>
    </item>
    <item>
      <title>
        <![CDATA[ Tayeb ]]>
      </title>
      <link>2</link>
      <description>
        <![CDATA[ [email protected] ]]>
      </description>
      <category />
      <author>
        <![CDATA[ Tayeb ]]>
      </author>
      <guid>2</guid>
      <pubDate>Wed, 22 Jun 2022 20:20:14 +0000</pubDate>
    </item>
    <item>
      <title>
        <![CDATA[ Tanvir ]]>
      </title>
      <link>1</link>
      <description>
        <![CDATA[ [email protected] ]]>
      </description>
      <category />
      <author>
        <![CDATA[ Tanvir ]]>
      </author>
      <guid>1</guid>
      <pubDate>Wed, 22 Jun 2022 20:20:14 +0000</pubDate>
    </item>
  </channel>
</rss>

That's all. Hope you enjoyed this tutorial. Thanks for reading.๐Ÿ™‚