定制 arinasystems/laravel-json-response 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

arinasystems/laravel-json-response

Composer 安装命令:

composer require arinasystems/laravel-json-response

包简介

Easily generate a JSON response in laravel.

README 文档

README

Laravel JSON Response Cover


Latest Version on Packagist Build Status Quality Score Total Downloads MIT Software License

Laravel JSON Response

Extensible and powerful API response package for Laravel.

  • Localized Response
  • Presets Response Status
  • Attribute Builders
  • Data Transformers
  • JSON Exception Handler

Installation

You can install the package via composer:

composer require arinasystems/laravel-json-response

The package will automatically register its service provider.

You can optionally publish the config file with:

php artisan vendor:publish --tag="json-response:config"

Basic Usage

use ArinaSystems\JsonResponse\Facades\JsonResponse;

/**
 * Display a listing of products.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $products = Product::paginate(2);

    return JsonResponse::json('ok', ['data' => ProductResource::collection($products)]);
}

With just one line of code, your API client will get this response:

{
    "success": true,
    "code": 200,
    "http_code": 200,
    "locale": "en",
    "message": "ok",
    "data": [
        {
            "id": "7OLgzrB1QVBQWMNZ3Rx08a24wAGEjqYVbeV",
            "name": "Aquafina Plastic Water Gallon – 18.9 L",
            "in_stock": true,
            "in_cart": false
            ...
        },
        {
            "id": "g3RbPoyMmPg9zr7qGZjW50Lp61aDlwnVOkE",
            "name": "Aquafina Plastic Water Bottle – 1.5 L",
            "in_stock": true,
            "in_cart": true
            ...
        }
    ],
    "additional": null,
    "links": {
        "first": "http://example.com/api/customer/products?page=1",
        "last": "http://example.com/api/customer/products?page=2",
        "prev": null,
        "next": "http://example.com/api/customer/products?page=2"
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 2,
        "path": "http://example.com/api/customer/products",
        "per_page": "2",
        "to": 2,
        "total": 3
    }
}

Configuration

You can customize the response configuration from config/json-response.php file.

<?php

return [
    'attributes'           => [
        'success'    => [
            'builder'     => \ArinaSystems\JsonResponse\Builders\SuccessAttributeBuilder::class,
            'value'       => true,
            'on-response' => true,
            'on-error'    => true,
        ],
        'code'       => [
            'builder'     => null,
            'value'       => 200,
            'on-response' => true,
            'on-error'    => true,
        ],
        'http_code'  => [
            'builder'     => null,
            'value'       => 200,
            'on-response' => true,
            'on-error'    => true,
        ],
        'locale'     => [
            'builder'     => \ArinaSystems\JsonResponse\Builders\LocaleAttributeBuilder::class,
            'value'       => null,
            'on-response' => true,
            'on-error'    => true,
        ],
        'message'    => [
            'builder'     => \ArinaSystems\JsonResponse\Builders\MessageAttributeBuilder::class,
            'value'       => null,
            'on-response' => true,
            'on-error'    => true,
        ],
        'data'       => [
            'builder'     => \ArinaSystems\JsonResponse\Builders\DataAttributeBuilder::class,
            'value'       => null,
            'on-response' => true,
            'on-error'    => false,
        ],
        'headers'    => [
            'value'       => [],
            'on-response' => false,
            'on-error'    => false,
        ],
        'exception'  => [
            'builder'     => \ArinaSystems\JsonResponse\Builders\ExceptionAttributeBuilder::class,
            'value'       => null,
            'on-response' => false,
            'on-error'    => true,
        ],
        'errors'     => [
            'builder'     => \ArinaSystems\JsonResponse\Builders\ErrorsAttributeBuilder::class,
            'value'       => [],
            'on-response' => false,
            'on-error'    => true,
        ],
        'debug'      => [
            'builder'     => \ArinaSystems\JsonResponse\Builders\DebugAttributeBuilder::class,
            'value'       => null,
            'on-response' => false,
            'on-error'    => true,
        ],
        'additional' => [
            'builder'     => null,
            'value'       => null,
            'on-response' => true,
            'on-error'    => true,
        ],
        'links'      => [
            'builder'     => null,
            'value'       => null,
            'on-response' => true,
            'on-error'    => true,
        ],
        'meta'       => [
            'builder'     => null,
            'value'       => null,
            'on-response' => true,
            'on-error'    => true,
        ],
    ],

    'status'               => [
        'ok' => \ArinaSystems\JsonResponse\Status\OkStatus::class,
    ],

    'transformers'         => [
        \Illuminate\Database\Eloquent\Model::class          => \ArinaSystems\JsonResponse\Transformers\EloquentTransformer::class,
        \Illuminate\Http\Resources\Json\JsonResource::class => \ArinaSystems\JsonResponse\Transformers\JsonResourceTransformer::class,
    ],

    'encoding_options'     => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE,
    'debug'                => env('APP_DEBUG', false),
    'message_translations' => true,
];

Response Structure

The first section in the configuration file allows you to set up the normal response and error response attributes. Each attribute array contains four options:

  • builder: Used to build the attribute value. (builder class or null to disable)
  • value: It's a default value if not passed in. (mixed)
  • on-response: Determines whether this attribute will appear in normal response. (boolean)
  • on-error: Determines whether this attribute will appear in error response. (boolean)

Preset Status

In transformers array define the preset statuses in the "Status" array. The key defines the status name, and the value is the name of the case class.

Data Transformers

Specify the data transformer for the given object type as a key and the transformer class as the value.

JSON Encoding Options

Set the json encoding options in 'encoding_options'.

Debugging Mode

Enable/Disable the debugging mode in 'debug' option.

Message Translations

Enable/Disable the locale response message in 'message_translations' option.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email info@arinasystems.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

arinasystems/laravel-json-response 适用场景与选型建议

arinasystems/laravel-json-response 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.76k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 06 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「arinasystems」 「json-response」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 arinasystems/laravel-json-response 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 arinasystems/laravel-json-response 我们能提供哪些服务?
定制开发 / 二次开发

基于 arinasystems/laravel-json-response 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-06-24