thuraaung2493/laravel-api-helpers

A simple package for creating API endpoints with Laravel.

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

This package is auto-updated.

Last update: 2025-01-10 07:30:26 UTC


README

It supports Laravel 9+ and PHP 8.1+

Description

This package contains many utility classes that assist in API creation.

Installation

Laravel

Require this package with composer using the following command:

composer require thuraaung2493/laravel-api-helpers@dev

Usage

Helper Classes for Json API Responses

All API Response classes implement the Responsable interface.

Response Formats see in 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
}

Middleware Classes

To change default headers in config

php artisan vendor:publish --tab=api-helpers

DataObjects

Example

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

Enums