homesheer/laravel-assembler
Composer 安装命令:
composer require homesheer/laravel-assembler
包简介
eloquent model or DTO assembler for Laravel
关键字:
README 文档
README
Introduction
Laravel Assembler is used to retrieve specifying fields of Eloquent Model or DTO for front-end requests, reducing the need for API interface upgrades.
Requirements
This package requires Laravel 5.4 or newer.
Installation
You can install the package via Composer:
composer require homesheer/laravel-assembler
You can publish the config file with:
php artisan vendor:publish --provider="HomeSheer\LaravelAssembler\AssemblerServiceProvider" --tag="config"
For Laravel 5.4 or older:
// config/app.php HomeSheer\LaravelAssembler\AssemblerServiceProvider::class,
For Lumen:
// bootstrap/app.php $app->register(HomeSheer\LaravelAssembler\AssemblerServiceProvider::class);
Usage
1. Instantiate Assembler or a subclass inherited from Assembler and pass in Eloquent model or DTO:
$assembler = new Assembler(User::find($id));
2. Call the getAssembledData method of Assembler to obtain the assembled data:
$assembledUserData = $assembler->getAssembledData();
3. Add the query string fields to the URL and specify the fields to be acquired:
The controller method of request returns the $assembledUserData of the previous step.
return $this->response($assembledUserData);
Request API interface to specify fields in the fields query string
// Get name and gender field information for users with ID 1
Http://localhost/users/1?fields={name,gender}
4. Attributes that do not exist in Eloquent Model or DTO can also be obtained:
Simply create a new class that inherits from Assembler and defines the method of getting the virtual attributes
class UserAssembler extends Assembler { public function getVirtualField() { return 'VirtualField'; } }
Then add the virtual field to the request
http://localhost/users/1?fields={name,gender,virtualField}
5. Supports three types of source data: objects with getter methods, objects without getter methods but with public attributes, and associative arrays:
Objects with getter methods:
class User { public function getName() { return 'Mars'; } }
Objects without getter methods but with public attributes:
class User { public $name = 'Mars'; }
Association array:
$user = [ 'name' => 'Mars', ];
6. Priority:
The acquired field is retrieved from high to low in the following order until the field terminates and returns null by default if it is not retrieved.
- Customize the
gettermethod inAssembler - The
gettermethod inEloquent ModelorDTO - Attributes in
Eloquent ModelorDTO
7. Nested Cascade:
In the case of Eloquent Model, natural support is provided for acquiring associated model attributes, as well as customized getter methods or attributes to return objects or arrays.
// Address is the associated object of user model
http://localhost/users/1?fields={name,gender,address{province,city}}
In the case of DTO, you need to define your own getter method or attribute that returns objects or arrays.
If it's an associative array, it's just a multidimensional associative array.
For response speed, it is not recommended to nest more than five layers.
8. config
If a customized Assembler and maps in config/assembler.php configure the corresponding relationship, the corresponding getter method of getAssembledData is automatically obtained when the getAssembler method is called after instantiating the parent class Assembler.
Contributing
Contributions are welcome, thanks to y'all :)
License
Laravel Assembler is open-sourced software licensed under the MIT license.
homesheer/laravel-assembler 适用场景与选型建议
homesheer/laravel-assembler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「model」 「dto」 「assembler」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 homesheer/laravel-assembler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 homesheer/laravel-assembler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 homesheer/laravel-assembler 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP library for the MUMSYS project
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A simple library that allows transform any kind of data to native php data or whatever
Interfaces for web resource models and services to retrieve and create them
Laravel 5 - Repositories to the database layer
Data transfer objects
统计信息
- 总下载量: 29
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-02-12