承接 kodepandai/laravel-api-response 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kodepandai/laravel-api-response

最新稳定版本:v1.3.1

Composer 安装命令:

composer require kodepandai/laravel-api-response

包简介

Api Response helper for laravel.

README 文档

README

A helper package to return JSON Api Response in structured way.

By default, the structure of the response will look like this:

{
  "success": true, // it was successfull or not
  "title": "Users", // the title/headline/section 
  "message": "Active users only", // the message/description/highlight
  "data": { // if it was successfull
    // profile..
    // users..
    // products..
    // etc..
  },
  "errors": { // if it was not successfull
    // validation errors..
    // any other errors..
  }
}

Example:

{
  "success": true,
  "title": "Users",
  "message": "Succesfully create a user",
  "data": {
    "id": 1,
    "name": "John Doe",
    "address": "4th Semarang Raya",
  },
}

Install

$ composer require kodepandai/laravel-api-response:dev-beta

Requirements:

  • PHP ^8.1
  • Laravel ^10.0

Laravel ^11

After installation, register api response handler in bootstrap/app.php

use KodePandai\ApiResponse\ApiExceptionHandler;

return Application::configure(basePath: dirname(__DIR__))
    //...
    ->withExceptions(function (Exceptions $exceptions) {
        // dont report any api response exception
        $exceptions->dontReport([
            \KodePandai\ApiResponse\Exceptions\ApiException::class,
            \KodePandai\ApiResponse\Exceptions\ApiValidationException::class,
        ]);
        // api response exception handler for /api
        $exceptions->renderable(function (Throwable $e, Request $request) {
            if ($request->wantsJson() || $request->is('*api*')) {
                return ApiExceptionHandler::render($e, $request);
            }
        });
    });

Laravel ^10

After installation, register api response handler in app/Exceptions/Handler.php

use KodePandai\ApiResponse\ApiExceptionHandler;

class Handler extends ExceptionHandler
{
    protected $dontReport = [
        \KodePandai\ApiResponse\Exceptions\ApiException::class,
        \KodePandai\ApiResponse\Exceptions\ApiValidationException::class,
    ];

    public function register()
    {
        $this->renderable(function (Throwable $e, Request $request) {
            if ($request->wantsJson() || $request->is('*api*')) {
                return ApiExceptionHandler::render($e, $request);
            }
        });
    }
}

The above handler will automatically transform any exception and render as ApiResponse json response.

Config

Publish config file using vendor:publish command

$ php artisan vendor:publish --tag=api-response-config

Usage

TODO

Return Response

TODO

Throw Exception

TODO

Overriding response structure

TODO

Develop

  • To test run composer test.

统计信息

  • 总下载量: 775
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-06-12