norotaro / wn-blogapi-plugin
v1.0.1
2021-07-26 15:36 UTC
Requires
- norotaro/wn-rest-plugin: ^1.0
- winter/wn-blog-plugin: ^2.0
This package is auto-updated.
Last update: 2024-09-26 22:44:48 UTC
README
为从Winter Blog Plugin获取数据提供的简单JSON Rest API
安装
在项目的根目录中运行以下命令
composer require norotaro/wn-blogapi-plugin php artisan winter:up
端点
该插件提供了列表帖子、帖子详情和列表分类的端点。
列表帖子
使用api/norotaro/blogapi/posts端点获取最新博客文章列表。该端点接受以下查询参数
- page - 该值用于确定用户所在的页面。默认值为1。
- category - 要根据其过滤文章的类别别名。如果没有指定或值为空,则返回所有文章。
- perPage - 每页返回多少篇文章(支持自动分页)。默认值为30。
- sort - 用于文章排序顺序的列名和方向。默认值为published_at desc。
- exceptPost - 通过其别名或唯一ID忽略单个文章。被忽略的文章将不会包含在列表中,这对于显示其他/相关文章很有用。
- exceptCategories - 忽略来自以逗号分隔的类别列表中的文章,这些类别通过它们的唯一别名给出。被忽略的文章将不会包含在列表中。
响应是Laravel分页器实例的JSON序列化,该实例还具有category属性。该属性表示从数据库加载的博客分类对象。如果没有找到类别或未指定,则该属性值为null。
以下示例显示了使用curl的基本端点使用示例
curl --request GET '{base_uri}/api/norotaro/blogapi/posts'
以下示例显示了具有类别过滤的基本组件使用示例
curl --request GET '{base_uri}/api/norotaro/blogapi/posts?category={category_slug}'
在示例中,您需要将
{base_uri}和{category_slug}字符串替换为有效的值。
以下是一个JSON返回示例
{
"total": 50,
"per_page": 15,
"current_page": 1,
"last_page": 4,
"first_page_url": "http://winter.app?category=test&page=1",
"last_page_url": "http://winter.app?category=test&page=4",
"next_page_url": "http://winter.app?category=test&page=2",
"prev_page_url": null,
"path": "http://winter.app",
"from": 1,
"to": 15,
"data":[
{
// Post Object
},
{
// Post Object
}
],
"category": {
// Category Object
}
}
帖子详情
使用api/norotaro/blogapi/posts/{slug}获取博客文章对象。
返回的文章对象将加载categories、featured_images和content_images关系。
以下示例显示了使用curl的基本端点使用示例
curl --request GET '{base_uri}/api/norotaro/blogapi/posts/{slug}'
如果没有找到具有指定别名的文章,则将返回以下响应,状态码为404
{
"code": 404,
"message": "Not found"
}
列表分类
使用api/norotaro/blogapi/categories端点获取博客文章分类列表。该端点接受以下查询参数
- displayEmpty - 确定是否显示空类别。默认值为false。
以下示例显示了使用curl的基本端点使用示例
curl --request GET '{base_uri}/api/norotaro/blogapi/categories'