gdebrauwer/laravel-hateoas
Composer 安装命令:
composer require gdebrauwer/laravel-hateoas
包简介
Expose the authorization logic of your REST API using HATEOAS links on your Laravel API resources
README 文档
README
HATEOAS allows you to expose the authorization logic of your REST API. This package makes it easy to add HATEOAS links to your Laravel API resources.
Each resource has its HATEOAS links, and only the accessible links per resource are returned. If a link is not available on a resource, then the clients of your API can disable functionality linked to that HATEOAS link.
By default an array of links, in the following format, will be added to the JSON of a Laravel API resource:
{
"data": [
{
"id": 1,
"text": "Hello world!",
"_links": [
{
"rel": "self",
"type": "GET",
"href": "http://localhost/message/1"
},
{
"rel": "delete",
"type": "DELETE",
"href": "http://localhost/message/1"
}
]
}
]
}
Installation
You can install the package via composer:
composer require gdebrauwer/laravel-hateoas
Usage
You can create a new HATEOAS class for a model using the following artisan command:
php artisan make:hateoas MessageHateoas --model=Message
In the created class you can define public methods that will be used to generate the links. A method should either return a link or null.
class MessageHateoas { use CreatesLinks; public function self(Message $message) : ?Link { if (! auth()->user()->can('view', $message)) { return; } return $this->link('message.show', ['message' => $message]); } public function delete(Message $message) : ?Link { if (! auth()->user()->can('delete', $message)) { return $this->link('message.archive', ['message' => $message]); } return $this->link('message.destroy', ['message' => $message]); } }
To add the links to an API resource, you have to add the HasLinks trait and use the $this->links() method. The HATEOAS class will be automatically discovered.
class MessageResource extends JsonResource { use HasLinks; public function toArray($request) : array { return [ 'id' => $this->id, 'text' => $this->text, '_links' => $this->links(), ]; } }
Customization
Formatting
You can customize the JSON links formatting by providing a formatter class that implements the Formatter interface to the formatLinksUsing method.
If the code to format the links is pretty small or you don't want to create a separate formatter class for it, you also have the option to provide a formatting callback function to the formatLinksUsing method.
use GDebrauwer\Hateoas\Hateoas; use GDebrauwer\Hateoas\LinkCollection; // Provide your own Formatter class ... Hateoas::formatLinksUsing(CustomFormatter::class); // ... Or provide a callback Hateoas::formatLinksUsing(function (LinkCollection $links) { // return array based on links });
HATEOAS class discovery
By default, the HATEOAS classes of models will be auto-discovered. Specifically, the HATEOAS classes must be in a Hateoas directory below the directory that contains the models. If you would like to provide your own HATEOAS class discovery logic, you can register a custom callback:
use GDebrauwer\Hateoas\Hateoas; Hateoas::guessHateoasClassNameUsing(function (string $class) { // return a HATEOAS class name });
Testing
composer test
Linting
composer lint
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
gdebrauwer/laravel-hateoas 适用场景与选型建议
gdebrauwer/laravel-hateoas 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 99.21k 次下载、GitHub Stars 达 174, 最近一次更新时间为 2019 年 08 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「HATEOAS」 「laravel」 「rest-api」 「gdebrauwer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gdebrauwer/laravel-hateoas 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gdebrauwer/laravel-hateoas 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gdebrauwer/laravel-hateoas 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PHP implementation of HAL http://stateless.co/hal_specification.html
Add the resource system to symfony application
Simple resource and serialization system for application
Integration of php-rest-api into Symfony.
A PSR-7 compatible library for making CRUD API endpoints
Integration of Hateoas into Symfony2.
统计信息
- 总下载量: 99.21k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 174
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-08-15