jongoly / api-standardizer
Standardized JSON API responses and global exception handling for Laravel.
v1.0.1
2026-09-04 19:41 UTC
Requires
- php: ^8.2|^8.3|^8.4
- illuminate/support: ^11.0|^12.0|^13.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
provides a unified trait to standardizes success, error payload.
A lightweight Laravel package to force consistent, clean, and predictable JSON structures across your API endpoints using a single trait.
- standardizes all success and error responses.
- lightweight: no external dependencies outside Laravel core framework.
Installation
via Composer:
composer require jongoly/api-standardizer
Publish Configuration
php artisan vendor:publish --tag="api-standardizer-config"
This creates a config/api-standardizer.php file in your application root:
return [ 'keys' => [ 'success' => 'success', 'data' => 'data', 'message' => 'message', 'errors' => 'errors', 'pagination' => 'pagination' ], ];
Usage
namespace App\Http\Controller; use App\Models\User; use Jongoly\ApiStandardizer\Concerns\ApiResponse; class UserController extends Controller { use ApiResponse; public function index() { \$users = User::paginate(10); // Return a standardized success response return this->success(users, 'Users fetched successfully.', 200); } }
Success Response Example
return this->success(data, 'Users fetched successfully.');
Output Json (Status 200):
{
"success": true,
"message": "Users fetched successfully.",
"data": [
{"id": 1, "name": "Ali"},
{"id": 2, "name": "Malik"}
],
"pagination": {
"total": 20,
"count": 2,
"per_page": 10,
"current_page": 1,
"total_pages": 2,
"has_more": true
}
}
Errir Response Example
return $this->error('Validation Failed.', 422, [ 'The Email must be valid.' ]);
Output Json(Status 422):
{
"success": false,
"message": "Validation Failed.",
"errors": {
"email": [
"The Email must be valid."
]
}
}
---