ajz/api-response 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

ajz/api-response

Composer 安装命令:

composer require ajz/api-response

包简介

Elegant and consistent API response helpers for Laravel applications with proper HTTP status codes and standardized JSON responses.

README 文档

README

Latest Version on Packagist Total Downloads Tests License

A elegant and consistent API response helper package for Laravel applications. Simplify your API responses with standardized JSON formats and proper HTTP status codes.

Requirements

  • PHP 8.3 or higher
  • Laravel 11.0 or higher

Installation

You can install the package via composer:

composer require ajz/api-response

Usage

Add the ApiResponseHelpers trait to your controller:

use Ajz\ApiResponse\Traits\ApiResponseHelpers;

class ApiController extends Controller
{
    use ApiResponseHelpers;
    
    public function index()
    {
        return $this->respondWithSuccess(['data' => $users]);
    }
}

Available Methods

Success Responses

// Return 200 with default success response
return $this->respondWithSuccess();

// Return 200 with custom data
return $this->respondWithSuccess(['data' => $users]);

// Return 200 with simple message
return $this->respondOk('Operation completed successfully');

// Return 201 for resource creation
return $this->respondCreated(['id' => $user->id]);

// Return 204 for no content
return $this->respondNoContent();

Error Responses

// Return 404 Not Found
return $this->respondNotFound('User not found');

// Return 401 Unauthorized
return $this->respondUnAuthenticated('Please login');

// Return 403 Forbidden
return $this->respondForbidden('Not allowed');

// Return 400 Bad Request
return $this->respondError('Invalid parameters');

// Return 422 Validation Error
return $this->respondFailedValidation('Validation failed');

Custom Success Response

You can customize the default success response:

// In your constructor or middleware
$this->setDefaultSuccessResponse(['status' => 'ok']);

// Now respondWithSuccess() will use this format
return $this->respondWithSuccess(); // Returns: {"status": "ok"}

Supported Data Types

The package supports various data types for responses:

  • Arrays
  • Laravel Collections
  • Objects implementing Arrayable
  • Objects implementing JsonSerializable
// Using with Laravel Collection
$users = User::all();
return $this->respondWithSuccess($users);

// Using with API Resource
$resource = UserResource::make($user);
return $this->respondWithSuccess($resource);

Response Format Examples

Success Response

{
    "success": true
}

Success Response with Data

{
    "data": {
        "id": 1,
        "name": "John Doe",
        "email": "john@example.com"
    }
}

Error Response

{
    "error": "Resource not found"
}

Validation Error Response

{
    "message": "The given data was invalid"
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.


### Additional Files

You might also want to create these supporting files:

1. `CHANGELOG.md`:
```markdown
# Changelog

All notable changes to `laravel-api-helpers` will be documented in this file

## 1.0.0 - 202X-XX-XX

- initial release
  1. CONTRIBUTING.md:
# Contributing

Contributions are welcome and will be fully credited.

Please read and understand the contribution guide before creating an issue or pull request.

## Etiquette

This project is open source, and as such, the maintainers give their free time to build and maintain the source code held within. They make the code freely available in the hope that it will be of use to other developers. It would be extremely unfair for them to suffer abuse or anger for their hard work.

Please be considerate towards maintainers when raising issues or presenting pull requests.

## Requirements

If the project maintainer has any additional requirements, you will find them listed here.

- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer).

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

## Happy coding!

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-13