当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

laravel-feed

Easily generate RSS feeds
授权协议 MIT License
开发语言 PHP
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 葛泳
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Generate RSS feeds in a Laravel app

This 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 us

We 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.

Installation

You can install the package via composer:

composer require spatie/laravel-feed

Register the routes the feeds will be displayed on using the feeds-macro.

// 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"

Usage

Imagine you have a model named NewsItem that contains records that you want to have displayed in the feed.

First you must implement the Feedable interface on that model. Feedable expects one method: toFeedItem, which should return a FeedItem instance.

// 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 inthe 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 renderedin 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 items key must point to a method that returns one of the following:

  • An array or collection of Feedables
  • An array or collection of FeedItems
  • An array or collection of arrays containing feed item values

Customizing your feed views

This package provides, out of the box, the feed::feed view that displays your feeds details.

However, you could use a custom view per feed by providing a view key inside of your feed configuration.

In the following example, we're using the previous News feed with a custom feeds.news view (located on resources/views/feeds/news.blade.php):

// 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 links

To 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 <head> through a partial view.

@include('feed::links')

As an alternative you can use this blade component:

<x-feed-links />

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

  • 官方文档: Laravel 基本信息:什么是 Laravel? | Laravel China 社区 Laravel 是 Taylor Otwell 开发的一款基于 PHP 语言的 Web 开源框架,采用了 MVC 的架构模式,在 2011 年 6 月正式发布了首个版本 —— 这是 Laravel 的第一个 Commit。 由于 Laravel 具备 Rails 敏捷开发等优秀特质,深度集成 PH

  • Laravel提供了 DB facade(原始查找) 查询构造器 Eloquent ORM 三种操作数据库方式 DB facade(原始查找)】 使用DB facade实现CURD //查询 $student = DB::select('select * from student'); echo '<pre>'; print_r($student); /

  • 1.redirect()->to() function to($to =null ,$status=302,$header=[],$secure=null) $secure 选择http 还是https 2.redirect()->route() 3.redirect()->back() 4 .home() 指向home路由 refresh() 刷新 away() secure()类似to()方法

  • 概述 环境:LNMP(CentOS7.2 + Nginx1.10 + MariaDB10.0 + PHP5.6),Laravel5.1; 目的:在Laravel框架上,用Beanstalkd消息队列实现异步发送邮件; 预备知识:Laravel框架基本编程;使用Laravel自带的Mail功能发送邮件; 正文 一般来说消息队列有两种场景,一种是发布者订阅者模式,一种是生产者消费者模式。本文讨论的通过

 相关资料
  • Laravel 是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。 功能特点 1、语法更富有表现力 你知道下面这行代码里 “true” 代表什么意思么? $uri = Uri::create(‘some/uri’, array(), array(), tr

  • 我需要空间/Laravel权限的帮助。当我试图分配它给我错误哎呀,看起来像出了问题。 错误 Connection.php第761行中的QueryExcema:SQLSTATE[23000]:完整性约束冲突:1048列role_id不能为空(SQL:插入到(,)值(9,))

  • Laravel 作为现在最流行的 PHP 框架,其中的知识较多,所以单独拿出来写一篇。 简述 Laravel 的生命周期 Laravel 采用了单一入口模式,应用的所有请求入口都是 public/index.php 文件。 注册类文件自动加载器 : Laravel通过 composer 进行依赖管理,无需开发者手动导入各种类文件,而由自动加载器自行导入。 创建服务容器:从 bootstrap/ap

  • 简介 Laravel Scout 为 Eloquent 模型 全文搜索提供了简单的,基于驱动的解决方案。通过使用模型观察者,Scout 会自动同步 Eloquent 记录的搜索索引。 目前,Scout 自带一个 Algolia 驱动;不过,编写自定义驱动很简单, 你可以轻松的通过自己的搜索实现来扩展 Scout。 安装 首先,通过 Composer 包管理器来安装 Scout: composer

  • 简介 Laravel 致力于让整个 PHP 开发体验变得愉快, 包括你的本地开发环境。 Vagrant 提供了一种简单,优雅的方式来管理和配置虚拟机。 Laravel Homestead 是一个官方预封装的 Vagrant box,它为你提供了一个完美的开发环境,而无需在本地机器安装 PHP 、Web 服务器和其他服务器软件。不用担心会搞乱你的操作系统!Vagrant boxes 是一次性的。如果

  • WebStack-Laravel 一个开源的网址导航网站项目,具备完整的前后台,您可以拿来制作自己的网址导航。 部署 克隆代码: git clone https://github.com/hui-ho/WebStack-Laravel.git 安装依赖: composer installphp artisan key:generate 编辑配置: cp .env.example .env ...D

  • 百度编辑器 For Laravel 5 支持自定义路由, 默认前后台独立控制器,支持重写方法方便自己的业务逻辑处理,支持扩展图片助手(推荐使用Intervention\Image第三方包) 官网 NinJa911工作室. 疑问讨论 请在issue里new一个. 授权 此Laravel 扩展包基于MIT协议开源MIT license. 安装 1.Composer 安装 composer requir

  • laravel-admin是一个基于laravel的后台管理开发框架,能帮助你使用很少的时间和代码量开发出功能完备的管理后台,另外它作为一个三方包,可以和框架内其它项目并行开发,真正做到前后台开发分离。 功能: RBAC权限管理模块。 菜单管理,和权限系统结合。 模型数据表格,能快速构建数据表格,并支持多种模型关系。 模型表单以及30+种form元素组件,自动实现增、删、改功能。 支持本地和云存储