pepperfm / api-responder-for-laravel
使用依赖注入(DI)的简单API响应模板
2.5.3
2024-08-04 22:02 UTC
Requires
- php: ^8.2
- guzzlehttp/guzzle: ^7.9
- guzzlehttp/psr7: ^2.6
- illuminate/database: >=10.0
- illuminate/http: >=10.0
- illuminate/support: >=10.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- laravel/pint: ^1.16
- orchestra/testbench: ^8.22
- pestphp/pest: ^2.32
- pestphp/pest-plugin-laravel: ^2.2
- phpunit/phpunit: ^10.0
- spatie/laravel-ray: ^1.37
Conflicts
- laravel/framework: <10.20.0
- dev-master
- 2.x-dev
- 2.5.3
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.9
- 2.4.8
- 2.4.7
- 2.4.6
- 2.4.5
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.5
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.1
- 2.1.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2024-09-04 22:10:19 UTC
README
使用依赖注入的简单API响应模板
提示
完整包描述:提供更好的或更简单的做事建议。
安装
您可以通过composer安装此包
composer require pepperfm/api-responder-for-laravel
用法
简单地使用laravel的DI功能。
使用部分
use Pepperfm\ApiBaseResponder\Contracts\ResponseContract;
然后是您喜欢的任何选项
public function __construct(public ResponseContract $json) { } public function index(Request $request) { $users = User::query()->whereIn('id', $request->input('ids'))->get(); return $this->json->response($users); }
用于分页
/* * Generate response.data.meta.pagination from first argument of paginated() method */ public function index(Request $request) { $users = User::query()->whereIn('id', $request->input('ids'))->paginate(); return $this->json->paginated($users); }
和一些数据映射
public function index(Request $request) { $users = User::query()->whereIn('id', $request->input('ids'))->paginate(); $dtoCollection = $users->getCollection()->mapInto(UserDto::class); return $this->json->paginated($dtoCollection->toArray(), $users); }
或者
public function index(Request $request) { $users = User::query()->whereIn('id', $request->input('ids'))->paginate(); $dtoCollection = $users->getCollection()->mapInto(UserDto::class); return $this->json->paginated($dtoCollection->toArray(), $users); } public function index(Request $request, ResponseContract $json) { return $json->response($users); } public function index(Request $request) { return resolve(ResponseContract::class)->response($users); }
或者您更愿意使用外观?
return \ApiBaseResponder::response($users); return BaseResponse::response($users);
响应中的分页数据
辅助函数 paginate
接受一个类型为 LengthAwarePaginator
的参数,并在后端返回格式为的对象
export interface IPaginatedResponse<T> { current_page: number per_page: number last_page: number data: T[] from: number to: number total: number prev_page_url?: any next_page_url: string links: IPaginatedResponseLinks[] } export interface IPaginatedResponseLinks { url?: any label: string active: boolean }
灵活的数据键
该包识别它被使用的方法,并根据REST,如果您从显示或编辑方法返回一条记录,则响应将在
response.data.entity
您可以在配置文件中自定义应返回此格式的方法,以及任何您喜欢的 数据键
还可以将任何响应数据键设置为您需要的任何方法,只需在控制器方法下添加属性 ResponseDataKey
来设置 response.data.entity
键,或传递任何字符串作为参数
#[ResponseDataKey] public function attributeWithoutParam(): JsonResponse { return $this->json->response($this->user); // response.data.entity } #[ResponseDataKey('random_key')] public function attributeWithParam(): JsonResponse { return $this->json->response($this->user); // response.data.random_key }
检查配置文件以自定义响应封装
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的更多信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全
如果您发现任何安全相关的问题,请通过电子邮件 Damon3453@yandex.ru 而不是使用问题跟踪器。
致谢
许可
MIT许可(MIT)。请参阅 许可文件 了解更多信息。
Laravel包模板
此包是用 Laravel包模板 生成的。