guanguans/laravel-api-response
Normalize and standardize Laravel API response data structure. - 规范化和标准化 Laravel API 响应数据结构。
Maintainers
Fund package maintenance!
4.0.1
2026-04-02 08:15 UTC
Requires
- php: >=8.2
- ext-json: *
- laravel/framework: ^11.51 || ^12.0 || ^13.0
- spatie/laravel-package-tools: ^1.93
Requires (Dev)
- adamwojs/php-cs-fixer-phpdoc-force-fqcn: ^2.0
- bamarni/composer-bin-plugin: ^1.9
- brainmaestro/composer-git-hooks: ^3.0
- canvural/larastan-strict-rules: ^3.0
- composer/semver: ^3.4
- driftingly/rector-laravel: ^2.2
- ergebnis/composer-normalize: ^2.50
- ergebnis/license: ^2.7
- ergebnis/php-cs-fixer-config: ^6.60
- ergebnis/phpstan-rules: ^2.13
- ergebnis/rector-rules: ^1.15
- guanguans/monorepo-builder-worker: ^3.1
- guanguans/php-cs-fixer-custom-fixers: ^1.2
- guanguans/phpstan-rules: ^1.1
- guanguans/rector-rules: ^1.7
- guzzlehttp/psr7: ^2.9
- jbelien/phpstan-sarif-formatter: ^1.2
- larastan/larastan: ^3.9
- laravel-lang/http-statuses: ^3.12
- laravel/facade-documenter: dev-main
- mockery/mockery: ^1.6
- mrpunyapal/peststan: ^0.2
- mrpunyapal/rector-pest: ^0.2
- nette/utils: ^4.1
- nicksdot/phpstan-phpstorm-error-identifiers: ^0.3
- orchestra/testbench: ^9.17 || ^10.0 || ^11.0
- pestphp/pest: ^3.8 || ^4.0
- pestphp/pest-plugin-arch: ^3.1 || ^4.0
- pestphp/pest-plugin-laravel: ^3.2 || ^4.0
- php-mock/php-mock-phpunit: ^2.15
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-mockery: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpstan/phpstan-webmozart-assert: ^2.0
- phpunit/phpunit: ^11.5 || ^12.0 || ^13.0
- povils/phpmnd: ^3.6
- rector/argtyper: ^0.6
- rector/jack: ^0.5
- rector/swiss-knife: ^2.3
- rector/type-perfect: ^2.1
- roave/backward-compatibility-check: ^8.14
- roave/no-floaters: ^1.13
- shipmonk/composer-dependency-analyser: ^1.8
- shipmonk/dead-code-detector: ^0.15
- shipmonk/name-collision-detector: ^2.1
- shipmonk/phpstan-baseline-per-identifier: ^2.3
- shipmonk/phpstan-rules: ^4.3
- sidz/phpstan-rules: ^0.5
- spatie/invade: ^2.1
- spaze/phpstan-disallowed-calls: ^4.9
- staabm/annotate-pull-request-from-checkstyle: ^1.8
- staabm/phpstan-todo-by: ^0.3
- staabm/side-effects-detector: ^1.0
- symfony/thanks: ^1.4
- symplify/coding-standard: ^13.0
- symplify/easy-coding-standard: ^13.0
- symplify/phpstan-rules: ^14.9
- tomasvotruba/class-leak: ^2.1
- tomasvotruba/cognitive-complexity: ^1.1
- tomasvotruba/ctor: ^2.2
- tomasvotruba/type-coverage: ^2.1
- tomasvotruba/unused-public: ^2.2
- yamadashy/phpstan-friendly-formatter: ^1.4
Suggests
- laravel-lang/http-statuses: Provide localized message for HTTP status code.
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-06-24 14:36:30 UTC
README
Normalize and standardize Laravel API response data structure. - 规范化和标准化 Laravel API 响应数据结构。
👁 tests
👁 php-cs-fixer
👁 codecov
👁 Latest Stable Version
👁 GitHub release (with filter)
👁 Total Downloads
👁 License
Features
- Support for customized response data structure
- Support for restful API response(optional)
- Support for automatically handled api exception
- Support for localized message
- Support for customized pipe(Process api response structure through pipeline)
- Support for customized exception pipe(Convert exception to api response through pipeline)
Requirement
- PHP >= 8.2
Installation
composer require guanguans/laravel-api-response --ansi -v
Configuration
Publish files(optional)
php artisan vendor:publish --provider="Guanguans\\LaravelApiResponse\\ServiceProvider" --ansi -v
Usage
Quick start
<?php namespace App\Http\Controllers\Api; use Guanguans\LaravelApiResponse\Support\Traits\ApiResponseFactory; use Illuminate\Http\JsonResponse; use Symfony\Component\HttpKernel\Exception\HttpException; class Controller extends \App\Http\Controllers\Controller { use ApiResponseFactory; public function examples(): JsonResponse { // return $this->apiResponse()->error($message); // return $this->apiResponse()->badRequest($message); // return $this->apiResponse()->unauthorized($message); // return $this->apiResponse()->exception($exception); // throw new \RuntimeException('This is a runtime exception.'); // throw new HttpException(400); // return $this->apiResponse()->success($data); // return $this->apiResponse()->noContent($message); // ... return $this->apiResponse()->ok(); } }
Default response structure(The following default response structure can be transformed through pipe)
{
"status": "bool",
"code": "int",
"message": "string",
"data": "mixed",
"error": "array<string, mixed>"
}
Default response examples
FAQ
Composer scripts
composer checks:required
composer php-cs-fixer:fix
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
Thanks
License
The MIT License (MIT). Please see License File for more information.
