mateusjatenee / php-json-feed
PHP项目。
1.0.6
2017-06-04 19:58 UTC
Requires
- php: >=7.0
- nesbot/carbon: ~1.20
Requires (Dev)
- illuminate/support: ~5.2
- orchestra/testbench: ~3.2
- phpunit/phpunit: ^6.1
README
该库提供了一种生成JSON feeds的方法,这是一种最近被社区引入的格式。
通过Composer安装
$ composer require mateusjatenee/php-json-feed
Laravel安装
在您的config/app.php文件中,注册服务提供者
'providers' => [ ... Mateusjatenee\JsonFeed\JsonFeedServiceProvider::class, ];
并在aliases数组中注册外观
'aliases' => [ ... 'JsonFeed' => Mateusjatenee\JsonFeed\Facades\JsonFeed::class, ];
用法
该库非常易于使用,实际上并不依赖于Laravel本身,尽管它允许您使用配置文件(尚未实现)。它将自动过滤格式化JSON并删除任何不必要的属性。
如果您不使用Laravel,请跳转到这部分。
如何使用?以下是一个JSON的示例
{
"title": "My JSON Feed test",
"home_page_url": "https://mguimaraes.co",
"feed_url": "https://mguimaraes.co/feeds/json",
"author": {
"url": "https://twitter.com/mateusjatenee",
"name": "Mateus Guimarães"
},
"icon": "https://mguimaraes.co/assets/img/icons/apple-touch-icon-72x72.png",
"favicon": "https://mguimaraes.co/assets/img/icons/favicon.ico",
"version": "https://jsonfeed.org/version/1",
"items": [
{
"content_text": "Great book. It's the best book.",
"date_published": "2017-05-22T00:00:00+00:00",
"title": "1984",
"author": {
"name": "Mateus",
"url": "https://mguimaraes.co"
},
"content_html": "<p>Great book. It's the best book.</p>",
"id": "abc123",
"url": "https://mguimaraes.co",
"external_url": "https://laravel.net.cn",
"date_modified": "2017-05-22T00:00:00+00:00"
},
{
"content_text": "Great book. It's the best book.",
"date_published": "2017-05-22T00:00:00+00:00",
"title": "1984",
"author": {
"name": "Mateus",
"url": "https://mguimaraes.co"
},
"content_html": "<p>Great book. It's the best book.</p>",
"id": "abc123",
"url": "https://mguimaraes.co",
"external_url": "https://laravel.net.cn",
"date_modified": "2017-05-22T00:00:00+00:00"
}
]
}
为此,首先需要设置配置——您可以在运行时(在服务提供者上,例如)使用外观或通过容器实例化它(即app('jsonFeed'))来设置它。
<?php use Mateusjatenee\JsonFeed\Facades\JsonFeed; $config = [ 'title' => 'My JSON Feed test', 'home_page_url' => 'https://mguimaraes.co', 'feed_url' => 'https://mguimaraes.co/feeds/json', 'author' => [ 'url' => 'https://twitter.com/mateusjatenee', 'name' => 'Mateus Guimarães', ], 'icon' => 'https://mguimaraes.co/assets/img/icons/apple-touch-icon-72x72.png', 'favicon' => 'https://mguimaraes.co/assets/img/icons/favicon.ico', ]; JsonFeed::setConfig($config);
然后,需要设置项目。项目可能是一个对象数组或对象集合。我们稍后再讨论这个问题。
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use JsonFeed; class JsonFeedController extends Controller { public function index() { $posts = App\Post::all(); return JsonFeed::setItems($posts)->toJson(); } }
一次性完成
或者,您可以一次性完成,尤其是如果您不使用Laravel。
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use JsonFeed; class JsonFeedController extends Controller { public function index() { $posts = App\Post::all(); $config = [ 'title' => 'My JSON Feed test', 'home_page_url' => 'https://mguimaraes.co', 'feed_url' => 'https://mguimaraes.co/feeds/json', 'author' => [ 'url' => 'https://twitter.com/mateusjatenee', 'name' => 'Mateus Guimarães', ], 'icon' => 'https://mguimaraes.co/assets/img/icons/apple-touch-icon-72x72.png', 'favicon' => 'https://mguimaraes.co/assets/img/icons/favicon.ico', ]; return JsonFeed::start($config, $posts)->toJson(); } }
如何获取每个项目的属性
这非常简单。一个项目只需要一个id,其他字段是可选的但强烈建议。首先,您的模型(或任何将在JSON feeds上使用的其他对象)需要实现\Mateusjatenee\JsonFeed\Contracts\FeedItemContract及其唯一方法——getFeedId()。它应返回相对于该项目的唯一ID。以下是每个方法和其功能的列表。
您可以在JSON Feed规范中找到所有接受的方法。
运行测试
$ composer test
许可证
该库采用MIT许可证。有关详细信息,请参阅LICENSE。
变更日志
有关详细信息,请参阅CHANGELOG。
贡献
有关详细信息,请参阅CONTRIBUTING。