PHPackages                             triquang/laravel-response-scaffold - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. triquang/laravel-response-scaffold

ActiveLibrary[API Development](/categories/api)

triquang/laravel-response-scaffold
==================================

Generate response scaffolding and exception handler.

2.0.0(9mo ago)08MITPHPPHP ^8.0

Since Aug 12Pushed 9mo agoCompare

[ Source](https://github.com/ntquangkk/laravel-response-scaffold)[ Packagist](https://packagist.org/packages/triquang/laravel-response-scaffold)[ RSS](/packages/triquang-laravel-response-scaffold/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

Laravel Model Doc Generator
===========================

[](#laravel-model-doc-generator)

[![Latest Version on Packagist](https://camo.githubusercontent.com/b88c9125fcd30882c56958789492a2a6d4e8be6fd57ab12cd02cc674da3cba0f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7472697175616e672f6c61726176656c2d726573706f6e73652d73636166666f6c642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/triquang/laravel-response-scaffold)[![Total Downloads](https://camo.githubusercontent.com/691cda9fb424e1ca94525a5d5b88cd0cf2e38431d6f907e513b446e691938404/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7472697175616e672f6c61726176656c2d726573706f6e73652d73636166666f6c642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/triquang/laravel-response-scaffold)[![License](https://camo.githubusercontent.com/870b808c26dc20fee0d71aa3176104eae524f8fdde78d6c1b2f72bc2c7dfee2e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7472697175616e672f6c61726176656c2d726573706f6e73652d73636166666f6c642e7376673f7374796c653d666c61742d737175617265)](https://github.com/ntquangkk/laravel-response-scaffold?tab=MIT-1-ov-file)

A Laravel Artisan command to quickly generate an API Response class, standardize response formats, and configure exception handling and guest redirects for APIs.

---

🚀 Features
----------

[](#-features)

- Create basic `routes/api.php` file if not exists.
- Create standardized `app/Support/Responses/ApiResponse.php` file if not exists.
- Configure **Global Exception Handler** in `bootstrap/app.php` to handle API errors:
    - `AuthenticationException` → 401 Unauthenticated
    - `AuthorizationException` → 403 Unauthorized
    - `ModelNotFoundException` → 404 Resource not found
    - `NotFoundHttpException` → 404 Endpoint not found
    - `ValidationException` → 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:
    - -&gt;withRouting()
    - -&gt;withExceptions()
    - -&gt;withMiddleware()

---

📦 Installation
--------------

[](#-installation)

Install via Composer (recommended to use --dev as this is a development tool):

```
composer require triquang/laravel-response-scaffold --dev
```

---

⚙️ Usage
--------

[](#️-usage)

Run the Artisan command:

```
php artisan make:response-scaffold
```

This command will:

1. Create `routes/api.php` if it does not exist.
2. Create `app/Support/Responses/ApiResponse.php` if it does not exist.
3. Add `Api route` configuration to `bootstrap/app.php`.
4. Add `redirectGuestsTo` configuration to `bootstrap/app.php`.
5. Add `Global Exception Handler` configuration to `bootstrap/app.php`.

---

📂 Output structure
------------------

[](#-output-structure)

After running, you will have:

```
app/
└── Support/
    └── Responses/
        └── ApiResponse.php
bootstrap/
└── app.php
routes/
└── api.php
```

---

💡 Examples
----------

[](#-examples)

> How to use:

1. Shorthand with parameter order
2. 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   //
