spatie/laravel-feed
Composer 安装命令:
composer require spatie/laravel-feed
包简介
Generate rss feeds
关键字:
README 文档
README
Generate RSS feeds in a Laravel app
This package provides an easy way to generate a feed for your Laravel application. Supported formats are RSS, Atom, and JSON. There's almost no coding required on your part. Just follow the installation instructions, update your 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.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require spatie/laravel-feed
Register the routes the feeds will be displayed on using the feeds-macro.
// In routes/web.php Route::feeds();
Optionally, you can pass a string as a first argument of the macro. The string will be used as a URL prefix for all configured feeds.
Next, you must publish the config file:
php artisan feed:install
Here's what that looks like:
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' => '', ], ], ];
Optionally you can publish the view files:
php artisan vendor:publish --provider="Spatie\Feed\FeedServiceProvider" --tag="feed-views"
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 Feedable interface on that model. Feedable expects one method: toFeedItem, which should return a FeedItem instance.
// app/NewsItem.php use Illuminate\Database\Eloquent\Model; use Spatie\Feed\Feedable; use Spatie\Feed\FeedItem; class NewsItem extends Model implements Feedable { public function toFeedItem(): FeedItem { return FeedItem::create() ->id($this->id) ->title($this->title) ->summary($this->summary) ->updated($this->updated_at) ->link($this->link) ->authorName($this->author) ->authorEmail($this->authorEmail); } }
If you prefer, returning an associative array with the necessary keys will do the trick too.
// app/NewsItem.php use Illuminate\Database\Eloquent\Model; use Spatie\Feed\Feedable; use Spatie\Feed\FeedItem; class NewsItem extends Model implements Feedable { public function toFeedItem(): FeedItem { return FeedItem::create([ 'id' => $this->id, 'title' => $this->title, 'summary' => $this->summary, 'updated' => $this->updated_at, 'link' => $this->link, 'authorName' => $this->authorName, ]); } }
Next, you'll have to create a method that will return all the items that must be displayed in the feed. You can name that method anything you like and you can do any query you want.
// app/NewsItem.php public static function getFeedItems() { return NewsItem::all(); }
Finally, you have to put the name of your class and the url where you want the feed to rendered in the config file:
// config/feed.php return [ 'feeds' => [ 'news' => [ /* * Here you can specify which class and method will return * the items that should appear in the feed. For example: * 'App\Model@getAllFeedItems' * or * ['App\Model', 'getAllFeedItems'] * * You can also pass an argument to that method. Note that their key must be the name of the parameter: * * ['App\Model@getAllFeedItems', 'parameterName' => 'argument'] * or * ['App\Model', 'getAllFeedItems', 'parameterName' => 'argument'] */ 'items' => 'App\NewsItem@getFeedItems', /* * The feed will be available on this url. */ 'url' => '/feed', 'title' => 'All newsitems on mysite.com', /* * The format of the feed. Acceptable values are 'rss', 'atom', or 'json'. */ 'format' => 'atom', /* * Custom view for the items. * * Defaults to feed::feed if not present. */ 'view' => 'feed::feed', ], ], ];
The items key must point to a method that returns one of the following:
- An array or collection of
Feedables - An array or collection of
FeedItems - An array or collection of arrays containing feed item values
Customizing your feed views
This package provides, out of the box, the feed::feed view that displays your feeds details.
However, you could use a custom view per feed by providing a view key inside of your feed configuration.
In the following example, we're using the previous News feed with a custom feeds.news view (located on resources/views/feeds/news.blade.php):
// config/feed.php return [ 'feeds' => [ 'news' => [ 'items' => ['App\NewsItem', 'getFeedItems'], 'url' => '/feed', 'title' => 'All newsitems on mysite.com', /* * The format of the feed. Acceptable values are 'rss', 'atom', or 'json'. */ 'format' => 'atom', /* * Custom view for the items. * * Defaults to feed::feed if not present. */ 'view' => 'feeds.news', ], ], ];
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="/feed">
You can add this to your <head> through a partial view.
@include('feed::links')
As an alternative you can use this blade component:
<x-feed-links />
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
spatie/laravel-feed 适用场景与选型建议
spatie/laravel-feed 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.91M 次下载、GitHub Stars 达 978, 最近一次更新时间为 2016 年 03 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rss」 「laravel」 「spatie」 「laravel-feed」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spatie/laravel-feed 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spatie/laravel-feed 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spatie/laravel-feed 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Receive webhooks in Laravel apps
Easily optimize images using PHP
Import an RSS-feed into blog
Store your application settings
Alfabank REST API integration
Add tags and taggable behaviour to your Laravel app
统计信息
- 总下载量: 3.91M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 988
- 点击次数: 23
- 依赖项目数: 35
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2016-03-03