Laravel 博客开发|RSS 订阅

发布日期 2022-06-02

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="{{url('projects')}}">项目</a>
        <a class="has-text-link" href="{{url('blog')}}">博客</a>
        <a class="has-text-link" href="{{url('about')}}">关于</a>
        <a class="has-text-link" href="{{url('sitemap')}}">地图</a>
        <a class="has-text-link" href="{{url('feed')}}">
            <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">滇ICP备17004667号-3</a>
    </div>
</footer>

注册路由

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

Route::feeds();

访问

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

站长:SevDot


SevDot 的头像

微信公众号