Laravel - How to generate RSS Feed using roumen/feed package?

By Hardik Savani April 16, 2024 Category : PHP Laravel

Today, I am going to share with you example of rss feed generator using roumen/feed package package in our laravel 5, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.

As we know, RSS feed is very basic requirement for any blog web-site, news-latter website, tutorial website etc. RSS feed help to inform new update of website using email. RSS feed through we can simple send email with new update of our website. Google feedburner, Mailchimp, Mailgun etc site take just rss feed and they manage automatic send email with latest update. So if you require to generate rss feed in Laravel then this post can help you best.

In this example, we are going to use roumen/feed package for generate rss feed in laravel application. roumen/feed package help to create rss feed very simple way. So, let's follow bellow step and generate rss feed. After successfully generate rss feed you will get layout like as bellow screen shot in firefox.

Preview:

Step 1 : Install Laravel Fresh Application

we are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Install Packages

In this step we have to install roumen/feed package by following command:

composer require roumen/feed

After successfully install package, open config/app.php file and add service provider.

config/app.php

'providers' => [

....

Roumen\Feed\FeedServiceProvider::class,

],

'aliases' => [

....

'Feed' => Roumen\Feed\Feed::class,

]

......

Now we have to publish configure file by using bellow command. This is optional.

php artisan vendor:publish --provider="Roumen\Feed\FeedServiceProvider"

Step 3: Add posts Table

we require to create new table "posts" that way we will get data from this table, you can use your own table but this is for example. we have to create migration for posts table using Laravel 5 php artisan command, so first fire bellow command:

php artisan make:migration create_post_table

After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create posts table.

<?

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;


class CreatePostTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('posts', function (Blueprint $table) {

$table->increments('id');

$table->string('title');

$table->string('slug');

$table->string('author');

$table->text('description');

$table->text('content');

$table->timestamps();

});

}


/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::drop("posts");

}

}

Ok, now we have to run migration using laravel artisan command:

php artisan migrate

Step 4: Create Route

In this step, we will create "rss-feed" route with how to generate rss feed, so let's add bellow route in our web.php file:

routes/web.php

Route::get('rss-feed', function () {


/* create new feed */

$feed = App::make("feed");


/* creating rss feed with our most recent 20 posts */

$posts = \DB::table('posts')->orderBy('created_at', 'desc')->take(20)->get();


/* set your feed's title, description, link, pubdate and language */

$feed->title = 'Your title';

$feed->description = 'Your description';

$feed->logo = 'http://yoursite.tld/logo.jpg';

$feed->link = url('feed');

$feed->setDateFormat('datetime');

$feed->pubdate = $posts[0]->created_at;

$feed->lang = 'en';

$feed->setShortening(true);

$feed->setTextLimit(100);


foreach ($posts as $post)

{

$feed->add($post->title, $post->author, URL::to($post->slug), $post->created_at, $post->description, $post->content);

}


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


});

Ok, now we are ready to run our rss feed generator example. Make sure you have some dummy records on posts table. So let's run bellow command on your root directory for quick run:

php -S localhost:8000

Now you can open bellow URL on your browser:

http://localhost:8000/rss-feed

You can get more information about this package from here : roumen/feed

I hope it can help you...

Shares