Easily generate RSS feeds
PHP HTML
Latest commit 4e21ef1 Jan 2, 2017 @ZengineChris ZengineChris committed with freekmurze Update LICENSE.md (#19)

README.md

Easily generate RSS feeds

Latest Version on Packagist Software License Build Status SensioLabsInsight Quality Score StyleCI Total Downloads

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.

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

Install

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,

];

Important note: The provider must be registered after your application's route service provider (App\Providers\RouteServiceProvider)

Then, you must register the routes the feeds will be displayed on using the feeds-macro. It's best to put this macro before registering any other routes.

// in your routes file
Route::feeds();

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\Model@getAllFeedItems'
             */
            'items' => '',

            /*
             * The feed will be available on this url.
             */
            'url' => '',

            'title' => 'My feed',

        ],
    ],

];

You can pass a string as a first argument of the macro. The string will be used as a prefix for the value specified in the url key of the config file.

Please note that you can register multiple feeds by having multiple items in the feeds-key.

Automatically generate feed links

To discover a feed, feed readers are looking 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::feed-links')

Usage

Imagine you have a model named NewsItem that contains records that you want to have displayed in the feed.

First you must implement the 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 action('NewsItemController@detail', [$this->url]);
    }

    public function getFeedItemAuthor() : string
    {
        return $this->author;
    }
}

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\Model@getAllFeedItems'
             */
            'items' => 'App\NewsItem@getFeedItems',

            /*
             * The feed will be available on this url.
             */
            'url' => '/feed',

            'title' => 'All newsitems on mysite.com',
        ],
    ],

];

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.

Credits

PHP 7

This package requires PHP 7, and we won't make a PHP 5 compatible version. We have good reasons to go PHP 7 only.

About Spatie

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.