triquang / laravel-response-scaffold
Generate response scaffolding and exception handler.
Installs: 8
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/triquang/laravel-response-scaffold
Requires
- php: ^8.0
- illuminate/support: ^11.0 || ^12.0
README
A Laravel Artisan command to quickly generate an API Response class, standardize response formats, and configure exception handling and guest redirects for APIs.
๐ Features
- Create basic
routes/api.phpfile if not exists. - Create standardized
app/Support/Responses/ApiResponse.phpfile if not exists. - Configure Global Exception Handler in
bootstrap/app.phpto handle API errors:AuthenticationExceptionโ 401 UnauthenticatedAuthorizationExceptionโ 403 UnauthorizedModelNotFoundExceptionโ 404 Resource not foundNotFoundHttpExceptionโ 404 Endpoint not foundValidationExceptionโ 422 Validation failed- Other errors โ 500 Server Error (or return detailed message if
APP_DEBUG=true)
- Prevent Laravel from automatically redirecting unlogged API to web login page.
- Works well on Laravel 11+ with configurations:
- ->withRouting()
- ->withExceptions()
- ->withMiddleware()
๐ฆ Installation
Install via Composer (recommended to use --dev as this is a development tool):
composer require triquang/laravel-response-scaffold --dev
โ๏ธ Usage
Run the Artisan command:
php artisan make:response-scaffold
This command will:
- Create
routes/api.phpif it does not exist. - Create
app/Support/Responses/ApiResponse.phpif it does not exist. - Add
Api routeconfiguration tobootstrap/app.php. - Add
redirectGuestsToconfiguration tobootstrap/app.php. - Add
Global Exception Handlerconfiguration tobootstrap/app.php.
๐ Output structure
After running, you will have:
app/
โโโ Support/
โโโ Responses/
โโโ ApiResponse.php
bootstrap/
โโโ app.php
routes/
โโโ api.php
๐ก Examples
How to use:
- Shorthand with parameter order
- Named parameters with arbitrary order (PHP 8+)
use App\Support\Responses\ApiResponse; class Example { public function shorthands() { // Just data ApiResponse::success($users); // With custom message ApiResponse::success($users, 'Users loaded'); // With custom status code ApiResponse::success($user, 'User created', 201); // Full parameters ApiResponse::success($users, 'Users found', 200, ['page' => 3]); // Error cases ApiResponse::error('Server error'); ApiResponse::error('User not found', [], 404); ApiResponse::error('Validation failed', $validator->errors(), 422); // With exception (debug) try { // some code } catch (Exception $e) { return ApiResponse::error('Something went wrong', [], 500, [], $e); } } public function namedParameters() { // Full parameters, in arbitrary order ApiResponse::success( data: $users, message: 'Users found', statusCode: 200, meta: ['page' => 3] ); // Error case ApiResponse::error( message: 'Something went wrong', errors: $validator->errors(), statusCode: 422, meta: ['s' => 2], exception: $e // <- in try-catch {}, if needed ); } }
๐ก Tip: Named parameters make your code more readable and avoid mistakes when skipping optional arguments.
When there is an error like NotFoundHttpException, the API will return:
{
"status": "error",
"message": "Endpoint not found",
"errors": [],
"meta": {
"url": "http://127.0.0.1:8000/api/not-exist"
}
}
โ Requirements
- PHP >= 8.0
- Laravel 11 / 12
- Composer
๐ License
MIT ยฉ Nguyแป n Trรญ Quang
๐ Contributing
PRs are welcome! Feel free to improve functionality or report issues via GitHub Issues.
๐ฌ Contact
- GitHub: github.com/ntquangkk
- Email: ntquangkk@gmail.com