I've set up multiple RSS feeds in the past, and with this package, it's never taken more than 10 minutes.
Let me guide you through the process of adding an RSS feed to an existing Laravel application, where each item is represented by a model record.
The spatie/laravel-feed package is easily the best way to set up an RSS feed.
Start by installing it.
composer require spatie/laravel-feed
Next, publish the config file. This is really important, and you won't be able to configure your feed properly without it.
php artisan feed:install
Once that's done, register the feeds
routes that handle all of your feeds. Just add to routes/web.php
.
Route::feeds();
Imagine you have a Campaign
model (representing email campaigns) and wanted these to be listed in a feed.
Head to that model and:
Feedable
interface on the model class.toFeedItem
method.use Spatie\Feed\Feedable;
use Spatie\Feed\FeedItem;
class Campaign extends Model implements Feedable
{
//...
public function toFeedItem(): FeedItem
{
return FeedItem::create()
->id($this->id)
->title($this->title)
->summary($this->teaser)
->updated($this->created_at)
->link(route('campaign.webview', $this->mailcoach_uuid))
->authorName('New in Laravel')
->authorEmail('alex@newinlaravel.com');
}
}
The toFeedItem
method configures (in a standardized way) all of the properties of each feed item. If you're unsure of a particular property, fill it with anything for now and return back once your feed is working to tweak it.
Within the same model, add a getFeedItems
method. This is responsible for fetching all the items you'd like to display in your feed.
Now's a great time to scope/order your query.
use Spatie\Feed\Feedable;
use Spatie\Feed\FeedItem;
class Campaign extends Model implements Feedable
{
public static function getFeedItems()
{
return Campaign::query()
->whereNotNull('sent_at')
->latest('sent_at')
->get();
}
public function toFeedItem(): FeedItem
{
return FeedItem::create()
->id($this->id)
->title($this->title)
->summary($this->teaser)
->updated($this->created_at)
->link(route('campaign.webview', $this->mailcoach_uuid))
->authorName('New in Laravel')
->authorEmail('alex@newinlaravel.com');
}
}
All that's left to do is choose where this feed will be available, and adjust some other properties.
Head to config/feed.php
. Here's the config file without any modifications:
return [
'feeds' => [
'main' => [
/*
* Here you can specify which class and method will return
* the items that should appear in the feed. For example:
* [App\Model::class, 'getAllFeedItems']
*
* You can also pass an argument to that method. Note that their key must be the name of the parameter:
* [App\Model::class, 'getAllFeedItems', 'parameterName' => 'argument']
*/
'items' => '',
/*
* The feed will be available on this url.
*/
'url' => '',
'title' => 'My feed',
'description' => 'The description of the feed.',
'language' => 'en-US',
/*
* The image to display for the feed. For Atom feeds, this is displayed as
* a banner/logo; for RSS and JSON feeds, it's displayed as an icon.
* An empty value omits the image attribute from the feed.
*/
'image' => '',
/*
* The format of the feed. Acceptable values are 'rss', 'atom', or 'json'.
*/
'format' => 'atom',
/*
* The view that will render the feed.
*/
'view' => 'feed::atom',
/*
* The mime type to be used in the <link> tag. Set to an empty string to automatically
* determine the correct value.
*/
'type' => '',
/*
* The content type for the feed response. Set to an empty string to automatically
* determine the correct value.
*/
'contentType' => '',
],
],
];
Each item under the feeds
array are each of your feeds (yes, you can have multiple!). For now, we'll just stick with the main
feed and adjust some details:
return [
'feeds' => [
'main' => [
/*
* Here you can specify which class and method will return
* the items that should appear in the feed. For example:
* [App\Model::class, 'getAllFeedItems']
*
* You can also pass an argument to that method. Note that their key must be the name of the parameter:
* [App\Model::class, 'getAllFeedItems', 'parameterName' => 'argument']
*/
'items' => 'App\Models\Campaign@getFeedItems',
/*
* The feed will be available on this url.
*/
'url' => '/feed',
'title' => 'New in Laravel RSS Feed',
'description' => null,
'language' => 'en-US',
/*
* The image to display for the feed. For Atom feeds, this is displayed as
* a banner/logo; for RSS and JSON feeds, it's displayed as an icon.
* An empty value omits the image attribute from the feed.
*/
'image' => '/favicon-96x96.png',
/*
* The format of the feed. Acceptable values are 'rss', 'atom', or 'json'.
*/
'format' => 'atom',
/*
* The view that will render the feed.
*/
'view' => 'feed::atom',
/*
* The mime type to be used in the <link> tag. Set to an empty string to automatically
* determine the correct value.
*/
'type' => '',
/*
* The content type for the feed response. Set to an empty string to automatically
* determine the correct value.
*/
'contentType' => '',
],
],
];
Lastly, head to yourapp.test/feed (or whatever URL you chose) and your should see your feed!
And that's how easy it is to add an RSS feed to your Laravel applications. If you need more information on the spatie/laravel-feed package, there's more over on the GitHub repository.