flarum/json-api-server
Composer 安装命令:
composer require flarum/json-api-server
包简介
A fully automated JSON:API server implementation in PHP.
README 文档
README
json-api-server is a JSON:API server implementation in PHP.
It allows you to build a feature-rich API by defining resource schema and connecting it to your application's database layer.
Based on your schema definition, the package will serve a complete API that conforms to the JSON:API specification, including support for:
- Showing individual resources (
GET /articles/1) - Listing resource collections (
GET /articles) - Sorting, filtering, pagination, and sparse fieldsets
- Compound documents with inclusion of related resources
- Creating resources (
POST /articles) - Updating resources (
PATCH /articles/1) - Deleting resources (
DELETE /articles/1) - Content negotiation
- Error handling
- Extensions including Atomic Operations
- Generating OpenAPI definitions
Documentation
Example
The following example uses an Eloquent model in a Laravel application. However, json-api-server can be used with any framework that can deal in PSR-7 Requests and Responses. Custom behavior can be implemented to support other ORMs and data persistence layers.
use App\Models\User; use Tobyz\JsonApiServer\Laravel; use Tobyz\JsonApiServer\Laravel\EloquentResource; use Tobyz\JsonApiServer\Laravel\Filter; use Tobyz\JsonApiServer\Endpoint; use Tobyz\JsonApiServer\Schema\Field; use Tobyz\JsonApiServer\Schema\Type; use Tobyz\JsonApiServer\JsonApi; class UsersResource extends EloquentResource { public function type(): string { return 'users'; } public function newModel(Context $context): object { return new User(); } public function endpoints(): array { return [ Endpoint\Show::make(), Endpoint\Index::make()->paginate(), Endpoint\Create::make()->visible(Laravel\can('create')), Endpoint\Update::make()->visible(Laravel\can('update')), Endpoint\Delete::make()->visible(Laravel\can('delete')), ]; } public function fields(): array { return [ Field\Attribute::make('name') ->type(Type\Str::make()) ->writable() ->required(), Field\ToOne::make('address')->includable(), Field\ToMany::make('friends') ->type('users') ->includable(), ]; } public function filters(): array { return [Filter\Where::make('id'), Filter\Where::make('name')]; } } $api = new JsonApi(); $api->resource(new UsersResource()); /** @var Psr\Http\Message\ServerRequestInterface $request */ /** @var Psr\Http\Message\ResponseInterface $response */ try { $response = $api->handle($request); } catch (Throwable $e) { $response = $api->error($e); }
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
License
flarum/json-api-server 适用场景与选型建议
flarum/json-api-server 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 96.24k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 03 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 flarum/json-api-server 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 flarum/json-api-server 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 96.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 13
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-03-08