triquang/laravel-response-scaffold
最新稳定版本:2.0.0
Composer 安装命令:
composer require triquang/laravel-response-scaffold
包简介
Generate response scaffolding and exception handler.
关键字:
README 文档
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
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-12