ninhtqse / architect
1.0
2021-03-04 08:16 UTC
Requires
- laravel/framework: ~6.0|~7.0|~8.0
Requires (Dev)
- mockery/mockery: 1.3.*
- orchestra/testbench: 4.*
- php-coveralls/php-coveralls: 2.2.*
- phpunit/phpunit: ~8.0
This package is auto-updated.
Last update: 2024-10-01 00:07:16 UTC
README
简介
Architect 用于动态创建 API 资源关系的结构。听起来很复杂,很有架势?
想象一下,你有一个名为 Book
的资源,与相关资源 Author
相关联。
Book 1-----n Author
正常嵌入式模式
这是使用嵌入式模式默认加载相关资源的方式。
{ "books":[ { "id":1, "author_id":1, "title":"How to save the world from evil", "pages":100, "author":{ "id":1, "name":"ninhtqse Prime" } }, { "id":2, "author_id":2, "title":"How to take over the world", "pages":100, "author":{ "id":2, "name":"Megatron" } } ] }
现在使用 Architect,你可以使用 ids
模式和 sideloading
模式来加载相关资源
Ids 模式
只加载相关资源的 ID。 使用 Eloquent 模型的主键属性将在路线图中。
{ "books":[ { "id":1, "author_id":1, "title":"How to save the world from evil", "pages":100, "author":1 }, { "id":2, "author_id":2, "title":"How to take over the world", "pages":100, "author":2 } ] }
Sideloading 模式
使用 ID 模式解析器将相关资源提升到全局作用域,并留下 ID。
{ "author":[ { "id":1, "name":"Ninhtqse Prime" }, { "id":2, "name":"Megatron" } ], "books":[ { "id":1, "author_id":1, "title":"How to save the world from evil", "pages":100, "author":1 }, { "id":2, "author_id":2, "title":"How to take over the world", "pages":100, "author":2 } ] }
用法
Architect 与普通数组(集合和资源)、Illuminate\Support\Collection
和 Illuminate\Database\Eloquent\Model
一起工作。
<?php $books = Book::with('Author')->get(); $architect = new \Ninhtqse\Architect\Architect; $parsed = $architect->parseData($books, [ 'author' => 'sideload' // can also be embed or ids (embed is default) ], 'books');
Ninhtqse\LaravelController 提供了方便的方法来在查询参数中定义 Architect 关系。
安装
composer require ninhtqse/architect
标准
此包符合 PSR-1、PSR-2 和 PSR-4。如果您发现遵守上的疏忽,请通过 pull request 发送补丁。
测试
$ phpunit
贡献
有关详细信息,请参阅 CONTRIBUTING。
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件。