定制 tyloo/api-response-helpers 二次开发

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

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

tyloo/api-response-helpers

Composer 安装命令:

composer require tyloo/api-response-helpers

包简介

A super simple package allowing for consistent API responses throughout your application

README 文档

README

run-tests Packagist Version Packagist PHP Version Packagist License

API Response Helpers

A simple package allowing for consistent API responses throughout your PHP application (Symfony, Laravel).

Requirements

  • PHP ^8.0

Installation / Usage

composer require tyloo/api-response-helpers

Simply reference the required trait within your controller:

<?php

namespace App\Controller;

use Tyloo\ApiResponseHelpers;
use Symfony\Component\HttpFoundation\JsonResponse;

class OrdersController
{
    use ApiResponseHelpers;

    public function index(): JsonResponse
    {
        return $this->respondWithSuccess();
    }
}

Optionally, the trait could be imported within a base controller.

Available methods

respondNotFound(string|Exception $message, ?string $key = 'error')

Returns a 404 HTTP status code, an exception object can optionally be passed.

respondWithSuccess(?array $contents = [])

Returns a 200 HTTP status code

respondOk(string $message)

Returns a 200 HTTP status code

respondUnAuthenticated(?string $message = null)

Returns a 401 HTTP status code

respondForbidden(?string $message = null)

Returns a 403 HTTP status code

respondError(?string $message = null)

Returns a 400 HTTP status code

respondCreated(?array $data = [])

Returns a 201 HTTP status code, with response optional data

respondNoContent(?array $data = [])

Returns a 204 HTTP status code, with optional response data. Strictly speaking, the response body should be empty. However, functionality to optionally return data was added to handle legacy projects. Within your own projects, you can simply call the method, omitting parameters, to generate a correct 204 response i.e. return $this->respondNoContent()

Use with additional object types

In addition to a plain PHP array, the following data types can be passed to relevant methods:

  • Objects implementing the native PHP JsonSerializable contract

This allows a variety of object types to be passed and converted automatically.

Below are a few common object types that can be passed.

Motivation

Ensure consistent JSON API responses throughout an application. The motivation was primarily based on a very old inherited project. The project contained a plethora of methods/structures used to return an error:

  • return new JsonResponse(['error' => $error], 400)
  • return new JsonResponse(['data' => ['error' => $error], 400)
  • return new JsonResponse(['message' => $error], Response::HTTP_BAD_REQUEST)
  • return new JsonResponse([$error], 400)
  • etc.

I wanted to add a simple trait that kept this consistent, in this case:

$this->respondError('Ouch')

Contribution

Any ideas are welcome. Feel free to submit any issues or pull requests.

Testing

vendor/bin/phpunit

Credits

License

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

tyloo/api-response-helpers 适用场景与选型建议

tyloo/api-response-helpers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 09 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 tyloo/api-response-helpers 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-09-20