发新包了,轻松实现网站 RSS 功能,遵循 RSS2.0 标准

moell/laravel-rss

moell/rss基础上开发的laravel包

源码地址

moell/rss
moell/laravel-rss

RSS规范

http://www.rssboard.org/rss-specification

要求

Laravel 5+

安装

composer require moell/laravel-rss:1.*

修改config/app.php

#在providers中追加
Moell\LaravelRss\RssServiceProvider::class,

#在aliases中追加
'Rss'   => Moell\LaravelRss\RssFacade::class,

提供接口

//设置字符集
public function setEncode($encode); //默认UTF-8

public function channel(array $channel);

public function item(array $item);

public function items(array $items);

//构造xml
public function build();

//快速构造
public function fastBuild(array $channel, array $item);

public function __toString();

用法

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use Rss;

class RssController extends Controller
{
    public function index()
    {
        $channel = [
            'title' => 'title',
            'link'  => 'http://moell.cn',
            'description' => 'description',
            'category' => [
                'value' => 'html',
                'attr' => [
                    'domain' => 'http://www.moell.cn'
                ]
            ]
        ];

        $rss = Rss::channel($channel);

        $items = [];
        for($i = 0; $i < 2; $i++) {
            $item = [
                'title' => "title".$i,
                'description' => 'description',
                'source' => [
                    'value' => 'moell.cn',
                    'attr' => [
                        'url' => 'http://www.moell.cn'
                    ]
                ]
            ];
            $items[] = $item;
            $rss->item($item);
        }

        return response($rss, 200, ['Content-Type' => 'text/xml']);

        //其他获取方式
        //return response($rss->build()->asXML(), 200, ['Content-Type' => 'text/xml']);

        //return response($rss->fastBuild($channel, $items)->asXML(), 200, ['Content-Type' => 'text/xml']);

        //return response($rss->channel($channel)->items($items)->build()->asXML(), 200, ['Content-Type' => 'text/xml']);

    }
}

生成结果

<?xml version="1.0" encoding="UTF-8"?>
<rss
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
        <title>title</title>
        <link>http://moell.cn</link>
        <description>description</description>
        <category domain="http://www.moell.cn">html</category>
        <item>
            <title>title0</title>
            <description>description</description>
            <source url="http://www.moell.cn">moell.cn</source>
        </item>
        <item>
            <title>title1</title>
            <description>description</description>
            <source url="http://www.moell.cn">moell.cn</source>
        </item>
    </channel>
</rss>

License

MIT

如果你喜欢本项目,欢迎star。GitHub

本帖已被设为精华帖!
本帖由 Summer 于 7年前 加精
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!