Laravel中快速构建API,了解一下laravel-restify
Laravel  /  管理员 发布于 2年前   704
Laravel Restify是一个使用 Laravel制作强大的JSON:API兼容的Rest API的包。
安装软件包并遵循设置指南后 , 您可以使用存储库 CLI 快速开始:
php artisan restify:repository Dream --all
存储库是这个包的核心。
上面的示例命令将生成一个空白存储库,您可以向其中添加字段,如下例所示:
namespace App\Restify;
use App\Models\Dream;
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
class DreamRepository extends Repository
{
public static string $model = Dream::class;
public function fields(RestifyRequest $request): array
{
return [
id(),
field('title')->required(),
field('description'),
field('image')->image(),
];
}
}
如果您不定义$model属性,Restify 可以根据存储库类名称(即模型)DreamRepository进行猜测,Dream模型
这是一个内置UserRepository类的示例
(您可能希望在真实应用中保护它),它将返回 JSON API 格式的 API 响应:
GET: /api/restify/users?perPage=10&page=2
{
"meta": {
"current_page": 1,
"from": 1,
"last_page": 1,
"path": "http://localhost:8000/api/restify/users",
"per_page": 15,
"to": 1,
"total": 1
},
"links": {
"first": "http://localhost:8000/api/restify/users?page=1",
"next": null,
"path": "http://localhost:8000/api/restify/users",
"prev": null,
"filters": "/api/restify/users/filters"
},
"data": [
{
"id": "1",
"type": "users",
"attributes": {
"name": "Paul Redmond",
"email": "[email protected]"
}
}
]
}
该软件包还引导您完成身份验证过程、高级过滤等!
相关知识链接
官方手册:
https://restify.binarcode.com/
git:
https://github.com/binarcode/laravel-restify
转:
https://laravel-news.com/laravel-restify
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号