Laravel 博客开发|RSS 订阅

RSS(简易信息聚合)是一种消息来源格式规范,用以聚合经常发布更新数据的网站,例如博客文章、新闻、音频或视频的网摘。RSS 文件(或称做摘要、网络摘要、或频更新,提供到频道)包含全文或是节录的文字,再加上发布者所订阅之网摘数据和授权的元数据。

这篇文章中与你分享,如何使用 spatie/laravel-feed扩展来实现 Laravel 博客 RSS 订阅。

安装扩展

运行如下命令安装扩展:

composer require spatie/laravel-feed

安装完成后运行如下命令发布配置文件:

php artisan vendor:publish --provider="Spatie\Feed\FeedServiceProvider" --tag="feed-config"

打开配置文件并修改为如下内容:

<?php 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' => 'App\Models\Article@getFeedItems',

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

            'title' => '关注独立开发和自由职业的个人博客',
            'description' => '订阅关注独立开发和自由职业的个人博客,每周写一篇回顾博客分享本周所做的事情和安排下周计划',
            'language' => 'zh_CN',

            /*
             * 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' => 'rss',

            /*
             * The view that will render the feed.
             */
            'view' => 'feed::rss',

            /*
             * 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="Spatie\Feed\FeedServiceProvider" --tag="feed-views"

修改文章模型

打开文章模型文件 app/Models/Article.php,添加 toFeedItemgetFeedItems方法

<?php namespace App\Models;

use Spatie\Feed\Feedable;
use Spatie\Feed\FeedItem;


class Article extends Model implements Feedable
{
     
    public function toFeedItem(): FeedItem
    {
        return FeedItem::create()
            ->id($this->id)
            ->title($this->title)
            ->summary($this->excerpt)
            ->updated($this->updated_at)
            ->link($this->link())
            ->authorName('SevDot')
            ->authorEmail('sevdot8@gmail.com');
    }

    public static function getFeedItems()
    {
        return Article::all();
    }

}

修改布局文件

打开 resources/views/layouts/app.blade.php文件,在 head里面添加如下内容:

@include('feed::links')

打开 resources/views/layouts/footer.blade.php文件,替换为如下内容:

<footer class="footer"><div class="has-text-centered">
        <a class="has-text-link" href="%7B%7Burl('projects')%7D%7D">项目</a>
        <a class="has-text-link" href="%7B%7Burl('blog')%7D%7D">博客</a>
        <a class="has-text-link" href="%7B%7Burl('about')%7D%7D">关于</a>
        <a class="has-text-link" href="%7B%7Burl('sitemap')%7D%7D">地图</a>
        <a class="has-text-link" href="%7B%7Burl('feed')%7D%7D">
            <i class="fa fa-feed"></i>
        </a>
    </div>
    <div class="has-text-centered">
        <span>©SevDot 2022. All rights reserved.</span>
        <a href="http://www.miitbeian.gov.cn/" target="_blank">ICP17004667-3</a>
    </div>
</footer>

注册路由

routes/web.php文件中添加如下路由:

Route::feeds();

访问

访问 http://blog.test/feed 可以看到如下页面:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SevDot

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值