GithubHelp home page GithubHelp logo

markcameron / laravel-feed Goto Github PK

View Code? Open in Web Editor NEW

This project forked from spatie/laravel-feed

0.0 1.0 0.0 101 KB

Easily generate RSS feeds

Home Page: https://murze.be/2016/03/a-package-to-easily-generate-feeds-in-laravel/

License: MIT License

PHP 88.50% HTML 11.50%

laravel-feed's Introduction

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 [email protected] 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.

laravel-feed's People

Contributors

freekmurze avatar sebastiandedeyne avatar markcameron avatar blueclock avatar ipalaus avatar zachleigh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.