RSS Feed Example in Laravel Without Packages
Every blogger needs to know that how to create an RSS feed and how to add an RSS feed to a website. So in this RSS feed example tutorial, I will explain that how we can create a very simple RSS feed 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. Actually, I am just going to show you a demo RSS feed so that you can customize it as your requirements.
We can use roumen/feed
package to create an RSS feed in Laravel. But without using any package, we will create an RSS feed in Laravel. Let's see how to create a simple demo RSS feed in Laravel without a package.
Step 1: Create Route
To create an RSS feed in Laravel, We need a route to generate an RSS feed in our blade file. So to know how to create an RSS feed in Laravel 8, follow the below steps.
routes/web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\RssFeedController;
Route::get('/feed', [RssFeedController::class,'generate_rss_feed'])->name('file');
Step 2: Create Controller
In this second step, We need to create our controller to get data for generating RSS data. You know that we need XML data to create an RSS feed.
app\Http\Controllers\RssFeedController.php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class RssFeedController extends Controller
{
public function generate_rss_feed()
{
$users = User::orderBy('id', 'desc')
->get();
return response()->view('feed', compact('users'))->header('Content-Type', 'application/xml');
}
}
Step 3: Create Blade File
Now we need a file feed.blade.php
to show our RSS feed XML data. Let's create that file.
resources/views/feed.blade.php
Keep remembering that it's just a demo. Use this pattern to create your own RSS feed in Laravel. Now if you visit this url http://127.0.0.1:8000/feed
, then you will see the below output:
Read also: Laravel Sitemap | Create Dynamic XML Sitemap in Laravel 7
Now you can create your own custom RSS feed for the Laravel application.