thuraaung2493/laravel-api-helpers

一个用于使用 Laravel 创建 API 端点的简单包。

dev-main 2023-09-10 04:26 UTC

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',
  // ]

枚举