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

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

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

f9webltd/laravel-api-response-helpers

Composer 安装命令:

composer require f9webltd/laravel-api-response-helpers

包简介

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

README 文档

README

Run Tests - Current Packagist Version Total Downloads Packagist PHP Version Packagist License

Laravel API Response Helpers

A simple package allowing for consistent API responses throughout your Laravel application. Originally created to solve issues having with a large legacy project, see the package motivation section.

Requirements

  • PHP ^8.2
  • Laravel ^12.0 or ^13.0

The package supports actively supported Laravel releases as per the official Laravel Support Policy.

Legacy versions of this package support older PHP / Laravel version, see the legacy section.

Installation / Usage

composer require f9webltd/laravel-api-response-helpers

Simply reference the required trait within your controller:

<?php

namespace App\Http\Api\Controllers;

use F9Web\ApiResponseHelpers;
use Illuminate\Http\JsonResponse;

class OrdersController
{
    use ApiResponseHelpers;

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

Optionally, the trait can 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|Arrayable|JsonSerializable|null $contents = null)

Returns a 200 HTTP status code, optionally $contents to return as json can be passed. By default returns ['success' => true].

respondOk(string $message)

Returns a 200 HTTP status code

respondUnAuthenticated(?string $message = null, ?string $key = 'error')

Returns a 401 HTTP status code

respondForbidden(?string $message = null, ?string $key = 'error')

Returns a 403 HTTP status code

respondError(?string $message = null, ?string $key = 'error')

Returns a 400 HTTP status code

respondCreated(array|Arrayable|JsonSerializable|null $data = null)

Returns a 201 HTTP status code, with response optional data

respondNoContent()

Returns a 204 HTTP status code, with an empty response body (RFC 9110)

public function respondAccepted(array|Arrayable|JsonSerializable|null $data = null)

Returns a 202 HTTP status code, with response optional data

public function respondConflict(?string $message = null, ?string $key = 'error')

Returns a 409 HTTP status code, with response optional message. If the message is omitted a default is sent

public function respondTooManyRequests(?string $message = null, ?string $key = 'error')

Returns a 429 HTTP status code, with response optional message. If the message is omitted a default is sent

setDefaultSuccessResponse(?array $content = null): self

Optionally, replace the default ['success' => true] response returned by respondWithSuccess with $content. This method can be called from the constructor (to change default for all calls), a base API controller or place when required.

setDefaultSuccessResponse is a fluent method returning $this allows for chained methods calls:

$users = collect([10, 20, 30, 40]);

return $this->setDefaultSuccessResponse([])->respondWithSuccess($users);

Or

public function __construct()
{
    $this->setDefaultSuccessResponse([]);
}

...

$users = collect([10, 20, 30, 40]);

return $this->respondWithSuccess($users);

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 Laravel Illuminate\Contracts\Support\Arrayable contract
  • 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.

Laravel Collections - Illuminate\Support\Collection

$users = collect([10, 20, 30, 40]);

return $this->respondWithSuccess($users);

Laravel Eloquent Collections - Illuminate\Database\Eloquent\Collection

$invoices = Invoice::pending()->get();

return $this->respondWithSuccess($invoices);

Laravel API Resources - Illuminate\Http\Resources\Json\JsonResource

This package is intended to be used alongside Laravel's API resources and in no way replaces them.

$resource = PostResource::make($post);

return $this->respondCreated($resource);

Legacy Support

For PHP ^8.2 with Laravel ^11.0 support, use package version ^3.1.

For PHP ^8.0 with Laravel ^8.0, ^9.0 and ^10.0 support use package version ^2.1.1.

For PHP ^7.4 and Laravel ^6.0 / ^7.0 support, use package version ^1.5.

Motivation

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

  • response()->json(['error' => $error], 400)
  • response()->json(['data' => ['error' => $error], 400)
  • response()->json(['message' => $error], Response::HTTP_BAD_REQUEST)
  • response()->json([$error], 400)
  • etc.

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

$this->respondError('Ouch')

Upgrading

Please see UPGRADING for details.

Contribution

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

Testing

composer test

Security

If you discover any security related issues, please email rob@f9web.co.uk instead of using the issue tracker.

Credits

License

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

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 644
  • Watchers: 9
  • Forks: 50
  • 开发语言: PHP

其他信息

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