In this tutorial, we’ll go over how to create RSS feeds in Laravel 9 using the roumen/feed package. Therefore, to create an RSS feed, we are utilizing the roumen/feed package.
We may quickly deliver a fresh update of our websites using the RSS feed. such as newsletters and updates to new articles, etc.
Although Google Feedburner, Mailchimp, and Mailgun all offer RSS feed functionality, you can also create RSS feeds using Laravel.
Table of Contents
- Install Laravel
- Setting Database Configuration
- Create Table using migration
- Install roumen/feed Package
- Add providers and aliases
- Create Route
- Create a Model and Controller
- Run Our Laravel Application
Install Laravel
We’re going to install Laravel 9, so first, open the terminal or command prompt and use it to navigate to the XAMPP htdocs folder directory. then execute the command below.
composer create-project --prefer-dist laravel/laravel laravel9_rss_feed
Setting Database Configuration
when Laravel has been fully installed. We must configure the database. We will now open the .env file and make the necessary changes to the database name, username, and password. Changes to a .env file can be seen below.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Enter_Your_Database_Name(laravel9_rss_feed)
DB_USERNAME=Enter_Your_Database_Username(root)
DB_PASSWORD=Enter_Your_Database_Password(root)
Create Table using migration
We must now start a migration. Consequently, we will use the command below to create a posts table migration.
php artisan make:migration create_posts_table --create=posts
following successful migration. The database/migrations/create posts table file needs to be modified as shown below.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('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::dropIfExists('posts');
}
}
?>
Once the above file has been modified, run the command below.
php artisan migrate
Install roumen/feed Package
Now, using the command below, we’ll install the roumen/feed package.
composer require roumen/feed
Add providers and aliases
In the “config/app.php” section, we will add the providers and aliases listed below.
'providers' => [
....
Roumen\Feed\FeedServiceProvider::class,
],
'aliases' => [
....
'Feed' => Roumen\Feed\Feed::class,
]
Create Route
In the “routes/web.php” file, add the following route code.
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
// return view('welcome');
});
Route::get('rss-feed' 'FeedController@rssFeed');
?>
Create a Model and Controller
The commands listed below assist in creating the controller and model.
php artisan make:model Post
php artisan make:controller FeedController
app/Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
//
protected $fillable = [
'title','slug', 'author','description','content'
];
}
?>
app/Http/Controllers/FeedController.php
<?php
namespace App\Http\Controllers;
use App;
use App\Post;
use Illuminate\Http\Request;
class FeedController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function rssFeed(Request $request)
{
/* create new feed */
$feed = App::make("feed");
/* Take out 15 posts from database to create feed */
$posts = Post::orderBy('created_at', 'desc')->take(15)->get();
/* Set feed's title, description, link, publish date and language */
$feed->title = 'Feedtitle';
$feed->description = 'Feed description';
$feed->logo = 'logo url';
$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');
}
}
?>
Run Our Laravel Application
The command listed below can be used to launch the server and run this example.
php artisan serve
http://127.0.0.1:8000/rss-feed