thuraaung2493 / laravel-api-helpers
一个用于使用 Laravel 创建 API 端点的简单包。
dev-main
2023-09-10 04:26 UTC
Requires
- php: ^8.2
- illuminate/support: ^10.12
- laravel/sanctum: ^3.2
- thecodingmachine/safe: ^2.5
Requires (Dev)
- laravel/pint: ^1.10
- nunomaduro/larastan: ^2.0
- nunomaduro/mock-final-classes: ^1.2
- orchestra/testbench: ^8.5
- pestphp/pest: ^2.6
- thecodingmachine/phpstan-safe-rule: ^1.2
This package is auto-updated.
Last update: 2024-09-10 06:53:37 UTC
README
它支持 Laravel 9+ 和 PHP 8.1+
描述
此包包含许多实用类,有助于创建 API。
安装
Laravel
使用以下命令通过 composer 安装此包
composer require thuraaung2493/laravel-api-helpers@dev
用法
Json API 响应的辅助类
所有 API 响应类都实现了 Responsable 接口。
响应格式见 responses.json
// Response Format for ModelResponse|CollectionResponse|MessageResponse|TokenResponse { "data": "", "message": "Success.", "status": 200 } // Response Format for ApiErrorResponse { "title": "Error!", "description": "Error Description", "status": 500 } // Response Format for ApiValidationErrorsResponse { "title": "Error!", "errors": [], "status": 422 }
中间件类
要更改配置中的默认头
php artisan vendor:publish --tab=api-helpers
数据对象
示例
$accept = Accept::of( type: ContentType::JSON ); // Turn to Header $accept->asHeader(); // new Header( // key: 'Accept', // value: 'application/json', // ); // Turn to headers array $accept->headers(); // [ // 'Accept' => 'application/json', // ]