This package provides an easy way to generate rss feeds. There's almost no coding required on your part. Just follow the installation instructions and provide some good values for the config file and you're good to go.
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
You can install the package via composer:
$ composer require spatie/laravel-feed
Next up, the service provider must be registered:
'providers' => [
...
Spatie\Feed\FeedServiceProvider::class,
];
Next, you must publish the config file:
php artisan vendor:publish --provider="Spatie\Feed\FeedServiceProvider"
This is the content of the published file laravel-feed.php:
return [
'feeds' => [
[
/*
* Here you can specify which class and method will return
* the items that should appear in the feed. For example:
* 'App\Repositories\NewsItemRepository@getAllOnline'
*/
'items' => '',
/*
* The feed will be available on this url.
*/
'url' => '',
'title' => 'My feed',
'description' => 'Description of my feed',
],
],
];
Please note that you can register multiple feeds by having multiple items in the feeds
-key.
To discover a feed, feed readers are look for a tag in the head section of your html documents that looks like this:
<link rel="alternate" type="application/atom+xml" title="News" href="linkToYourFeed" />
You can put that link manually in your template, but this package can also automate that for you. Just put this include in the head section of your template.
@include('laravel-feed::feeds-links')
Image you have a model named NewsItem
that contains records that you want to have displayed it the feed.
First you must implement FeedItem
interface on that model. Here's an example.
class NewsItem implements FeedItem
{
public function getFeedItemId()
{
return $this->id;
}
public function getFeedItemTitle() : string
{
return $this->title;
}
public function getFeedItemSummary() : string
{
return $this->text;
}
public function getFeedItemUpdated() : Carbon
{
return $this->last_updated;
}
public function getFeedItemLink() : string
{
return "https://mysite.com/news/{$this->url}";
}
}
Next, you'll have to create a method that will return all the newsItems that must be displayed in the feed. You can name that method anything you like and you can do any query you want.
//in your NewsItem model
public function getFeedItems()
{
return NewsItem::all();
}
And finally you have to put the name of your class and the url where you want the feed to rendered in the config file:
//app/config/laravel-feed
return [
'feeds' => [
[
/*
* Here you can specify which class and method will return
* the items that should appear in the feed. For example:
* 'App\Repositories\NewsItemRepository@getAllOnline'
*/
'items' => 'App\NewsItem@getFeedItems',
/*
* The feed will be available on this url.
*/
'url' => '/feed',
'title' => 'All newsitems on mysite.com',
'description' => 'Lorum ipsum Laravel is great bla bla bla',
],
],
];
Please see CHANGELOG for more information what has changed recently.
composer test
Please see CONTRIBUTING for details.
If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.
This package requires PHP 7, and we won't make a PHP 5 compatible version. We have good reasons to go PHP 7 only.
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
The MIT License (MIT). Please see License File for more information.