最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
laravel-feed:实践指南
时间:2026-09-11 11:48:01 编辑:袖梨 来源:一聚教程网
工作中遇到相关需求时,laravel-feed值得先读说明,因为它主要用于轻松生成 RSS 提要。把它放到日常自动化流程中,最容易暴露的是输入边界、依赖和失败处理如果不清楚就很难稳定复用,不能只看演示是否顺利。我的评估方法是用一项范围明确的真实任务完成最小试跑,然后检查配置时间、输出质量、异常信息和维护痕迹是否与文档一致。它对愿意先做小范围验证并复查原始文档的团队更有价值,但正式采用前仍要复查许可证、近期提交和问题区回应。

<picture>
</picture>
Generate RSS 在 Laravel 中提供 app
该包提供了一种为 Laravel 应用程序生成提要的简单方法。 支持的格式为 RSS、Atom 和 JSON。您几乎不需要编码。只需按照安装说明进行操作,更新您的配置文件,就可以开始了。
Spatie 是一家位于比利时安特卫普的网页设计机构。您可以在我们的网站 上找到我们所有开源项目 [的概述。
支持我们
我们投入了大量资源来创建 一流的开源包。您可以通过 购买我们的一款付费产品 来支持我们。
我们非常感谢您从家乡寄给我们一张明信片,并注明您正在使用我们的哪种套餐。您可以在 我们的联系页面 上找到我们的地址。我们将所有收到的明信片发布在 我们的虚拟明信片墙 上。
安装
您可以通过 Composer 安装该软件包:
composer require spatie/laravel-feed
使用 feeds- 宏注册将显示源的路由。
// In routes/web.php
Route::feeds();
或者,您可以传递一个字符串作为宏的第一个参数。该字符串将用作所有配置的 feed 的 URL 前缀。
接下来,您必须发布配置文件:
php artisan feed:install
看起来是这样的:
return [
'feeds' => [
'main' => [
/*
* Here you can specify which class and method will return
* the items that should appear in the feed. For example:
* [AppModel::class, 'getAllFeedItems']
*
* You can also pass an argument to that method. Note that their key must be the name of the parameter: *
* [AppModel::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' => '',
],
],
];
您可以选择发布视图文件:
php artisan vendor:publish --provider="SpatieFeedFeedServiceProvider" --tag="feed-views"
用途
假设您有一个名为 NewsItem 的模型,其中包含您想要在摘要中显示的记录。
首先,您必须在该模型上实现 Feedable 接口。 Feedable 需要一种方法:toFeedItem,它应该返回 FeedItem 实例。
// app/NewsItem.php
use IlluminateDatabaseEloquentModel;
use SpatieFeedFeedable;
use SpatieFeedFeedItem;
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);
}
}
如果您愿意,返回带有必要键的关联数组也可以达到目的。
// app/NewsItem.php
use IlluminateDatabaseEloquentModel;
use SpatieFeedFeedable;
use SpatieFeedFeedItem;
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,
]);
}
}
接下来,您必须创建一个方法,该方法将返回必须显示在中的所有项目 饲料。您可以将该方法命名为您喜欢的任何名称,并且可以执行您想要的任何查询。
// app/NewsItem.php
public static function getFeedItems()
{
return NewsItem::all();
}
最后,您必须将类的名称和要呈现提要的网址放在一起 在配置文件中:
// 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:
* 'AppModel@getAllFeedItems'
* or
* ['AppModel', 'getAllFeedItems']
*
* You can also pass an argument to that method. Note that their key must be the name of the parameter: *
* ['AppModel@getAllFeedItems', 'parameterName' => 'argument']
* or
* ['AppModel', 'getAllFeedItems', 'parameterName' => 'argument']
*/
'items' => 'AppNewsItem@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',
],
],
];
items 键必须指向返回以下值之一的方法:
Feedables 的数组或集合FeedItems 的数组或集合- 包含 Feed 项值的数组或数组集合
自定义您的 Feed 视图
此软件包提供开箱即用的 feed::feed 视图,用于显示您的源详细信息。
但是,您可以通过在 Feed 配置中提供 view 密钥来为每个 Feed 使用自定义视图。
在以下示例中,我们将之前的 News 源与自定义 feeds.news 视图(位于 resources/views/feeds/news.blade.php 上)结合使用:
// config/feed.php
return [
'feeds' => [
'news' => [
'items' => ['AppNewsItem', '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',
],
],
];
自动生成提要链接
要发现提要,提要阅读器会在 html 文档的 head 部分中查找如下所示的标签:
<link rel="alternate" type="application/atom+xml" title="News" href="/feed">
您可以通过部分视图将其添加到您的 <head> 中。
@include('feed::links')
作为替代方案,您可以使用此刀片组件:
<x-feed-links />
测试
composer test
安全漏洞
请查看 我们的安全正策 有关如何报告安全漏洞的信息。
相关文章
- MonoMod:实践指南 09-11
- 维西之王兑换码有哪些-这些礼包码别漏领 09-11
- lobe-vidol:AI Agent 工具实践指南 09-11
- GtkSharp:实践指南 09-11
- 仙遇小程序兑换码有哪些-这些礼包码别漏领 09-11
- VQ-Diffusion:实践指南 09-11