在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):spatie/laravel-feed开源软件地址(OpenSource Url):https://github.com/spatie/laravel-feed开源编程语言(OpenSource Language):PHP 90.0%开源软件介绍(OpenSource Introduction):Generate RSS feeds in a Laravel appThis 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 usWe 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. InstallationYou can install the package via composer: composer require spatie/laravel-feed Register the routes the feeds will be displayed on using the // 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 a URL prefix for all configured feeds. Next, you must publish the config file: php artisan vendor:publish --provider="Spatie\Feed\FeedServiceProvider" --tag="feed-config" 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" UsageImagine you have a model named First you must implement the // 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
Customizing your feed viewsThis package provides, out of the box, the However, you could use a custom view per feed by providing a In the following example, we're using the previous // 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 linksTo 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 @include('feed::links') As an alternative you can use this blade component: <x-feed-links /> Testingcomposer test ChangelogPlease see CHANGELOG for more information on what has changed recently. ContributingPlease see CONTRIBUTING for details. Security VulnerabilitiesPlease review our security policy on how to report security vulnerabilities. CreditsLicenseThe MIT License (MIT). Please see License File for more information. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论