nurbekjummayev/laravel-api-response-helpers
Composer 安装命令:
composer require nurbekjummayev/laravel-api-response-helpers
包简介
A Laravel package for standardized API responses
README 文档
README
A Laravel package for standardized API responses with helpful exceptions.
Installation
Install the package via composer:
composer require nurbekjummayev/laravel-api-response-helpers
Usage
Helper Functions
The package provides convenient helper functions for common HTTP responses:
Success Responses
// List with pagination public function index(Request $request) { $query = Product::query(); $data = $query->paginate($request->get('per_page', 10)); return okWithPaginateResponse($data); } // Show single resource public function show(int $id) { $model = Product::findOrFail($id); return okResponse($model); } // Create resource public function store(Request $request) { $model = Product::create($request->all()); return createdResponse($model); } // Update resource public function update(Request $request, int $id) { $model = Product::findOrFail($id); $model->update($request->all()); return okResponse($model); } // Delete resource public function destroy(int $id) { $model = Product::findOrFail($id); $model->delete(); return okResponse($model); }
Error Responses
// Validation error (422) $validator = Validator::make($request->all(), $rules); if ($validator->fails()) { return invalidData('Validation failed', ['errors' => $validator->errors()]); } // Not found (404) $model = Product::find($id); if (!$model) { return notFoundRequestResponse('Product not found'); } // Unauthorized (401) if (!auth()->check()) { return unauthorizedRequestResponse(); } // Forbidden (403) if (!auth()->user()->can('update', $model)) { return forbiddenRequestResponse('Access denied'); } // Bad request (400) if (!$request->has('required_field')) { return badRequestResponse('Missing required field'); } // Method not allowed (405) return methodNotAllowedRequestResponse(); // Payload too large (413) return postTooLargeResponse(); // Too many requests (429) return tooManyRequestsResponse(); // Server error (500) try { // Some operation } catch (\Exception $e) { return serverErrorResponse('Something went wrong'); } // Any custom status return errorResponse('Custom error', httpStatus: 418, errorMsg: 'TEAPOT');
Custom Response
return apiResponse( msg: 'Custom message', data: ['key' => 'value'], success: true, httpStatus: 200, errorMsg: null, extraData: ['meta' => ['version' => '1.0']] );
Exceptions
The package provides exception classes that automatically render as JSON responses:
use NurbekJummayev\ApiResponseHelper\Exceptions\NotFoundException; use NurbekJummayev\ApiResponseHelper\Exceptions\ForbiddenException; use NurbekJummayev\ApiResponseHelper\Exceptions\ValidationException; // Not Found Exception public function show(int $id) { $model = Product::find($id); if (!$model) { throw new NotFoundException('Product not found', ['product_id' => $id]); } return okResponse($model); } // Validation Exception public function store(Request $request) { $validator = Validator::make($request->all(), $rules); if ($validator->fails()) { throw new ValidationException( message: 'Validation failed', data: ['errors' => $validator->errors()] ); } $model = Product::create($request->all()); return createdResponse($model); } // Forbidden Exception public function update(Request $request, int $id) { $model = Product::findOrFail($id); if (!auth()->user()->can('update', $model)) { throw new ForbiddenException('You cannot update this product'); } $model->update($request->all()); return okResponse($model); }
Available Exceptions
ApiResponseException- Base exception classBadRequestException- 400UnauthorizedException- 401ForbiddenException- 403NotFoundException- 404MethodNotAllowedException- 405PostTooLargeException- 413ValidationException- 422TooManyRequestsException- 429ServerErrorException- 500
Note:
ValidationExceptionshares its short name with Laravel's ownIlluminate\Validation\ValidationException. If you need both in the same file, import this package's class with an alias:use NurbekJummayev\ApiResponseHelper\Exceptions\ValidationException as ApiValidationException;
Response Format
All responses follow a consistent structure:
Standard Response:
{
"msg": "Success message",
"error": null,
"success": true,
"data": {}
}
Paginated Response:
{
"msg": "OK",
"error": null,
"success": true,
"data": [
{
"id": 1,
"name": "Product 1"
},
{
"id": 2,
"name": "Product 2"
}
],
"meta": {
"current_page": 1,
"from": 1,
"last_page": 5,
"per_page": 15,
"to": 15,
"total": 75
}
}
okWithPaginateResponse() supports all three Laravel paginators:
paginate()— fullmetaas shown above.simplePaginate()—metawithcurrent_page,from,per_page,to,has_more(nototal/last_page).cursorPaginate()—metawithper_page,next_cursor,prev_cursor.
With Extra Data:
{
"msg": "Success",
"error": null,
"success": true,
"data": {},
"custom_key": "custom_value"
}
AI Support (Laravel Boost)
This package ships first-class AI support for Laravel Boost. When a project that uses Boost installs this package, the guidelines and an agent skill are discovered automatically.
What's included:
- Guideline —
resources/boost/guidelines/core.blade.php: a short, always-loaded overview of the response envelope and helpers. - Skill —
resources/boost/skills/api-response-helper/SKILL.md: the fullapi-response-helperskill, loaded on demand when an agent works on API responses.
In a consuming project that already has Boost installed:
composer require nurbekjummayev/laravel-api-response-helpers # Discover and publish this package's guidelines + skill php artisan boost:install # or, for an existing Boost setup: php artisan boost:update --discover
Boost then teaches the coding agent (Claude Code, Cursor, Copilot, etc.) to use okResponse(), okWithPaginateResponse(), the error helpers, and the renderable exceptions correctly. No configuration is required — discovery is based on the resources/boost/ directory.
Testing
composer test
Code Quality
# Format code composer format # Test coverage composer test-coverage
License
The MIT License (MIT). Please see License File for more information.
nurbekjummayev/laravel-api-response-helpers 适用场景与选型建议
nurbekjummayev/laravel-api-response-helpers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 120 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「api」 「response」 「helper」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nurbekjummayev/laravel-api-response-helpers 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nurbekjummayev/laravel-api-response-helpers 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nurbekjummayev/laravel-api-response-helpers 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
HTTP request logger middleware for Laravel
A PSR-7 compatible library for making CRUD API endpoints
Class to generate a standard structure for api json responses
统计信息
- 总下载量: 120
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 20
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-10