PHP code example of larahub / api-response-kit

1. Go to this page and download the library: Download larahub/api-response-kit library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

larahub / api-response-kit example snippets


// Controller action — no changes needed
return User::query()->latest()->paginate();

// Default per_page comes from config (default: 10)
return User::query()->latest()->paginate();

// Developer-defined per_page
return User::query()->latest()->paginate(15);

// Cursor pagination is also supported
return User::query()->latest()->cursorPaginate(10);

use LaraHub\ApiResponseKit\Facades\ApiResponseKit;

return ApiResponseKit::paginated(
    User::query()->latest()->paginate(15),
    'Users fetched successfully'
);

// Returns the value of pagination.per_page (default: 10)
return ApiResponseKit::paginated(
    User::query()->latest()->paginate(ApiResponseKit::perPage())
);

// config/api-response-kit.php
'pagination' => [
    'per_page' => env('API_RESPONSE_KIT_PER_PAGE', 10),
],

use LaraHub\ApiResponseKit\Facades\ApiResponseKit;

// Success responses
ApiResponseKit::success($data, 'User fetched');          // 200
ApiResponseKit::created($data, 'Resource created');      // 201
ApiResponseKit::accepted($data, 'Request accepted');     // 202
ApiResponseKit::noContent();                             // 204

// Error responses
ApiResponseKit::badRequest('Invalid input', $errors);    // 400
ApiResponseKit::unauthorized('Please log in');           // 401
ApiResponseKit::forbidden('Access denied');              // 403
ApiResponseKit::notFound('Resource not found');          // 404
ApiResponseKit::methodNotAllowed();                      // 405
ApiResponseKit::conflict('Duplicate entry', $errors);   // 409
ApiResponseKit::unprocessableEntity('Failed', $errors); // 422
ApiResponseKit::tooManyRequests();                       // 429
ApiResponseKit::serverError('Oops', $errors);           // 500
ApiResponseKit::serviceUnavailable();                   // 503

// Validation errors
ApiResponseKit::validationError($errors, 'Validation failed');

// Exceptions
ApiResponseKit::exception($throwable);

// Request ID
$id = ApiResponseKit::getRequestId();
ApiResponseKit::setRequestId('my-trace-id');

return [
    'debug' => [
        'show_trace' => env('APP_DEBUG', false),
        'hide_sql_errors' => env('APP_ENV') === 'production',
    ],
];

return [
    'formatter' => null,
    'custom_formatters' => [
        // 'my_custom_format' => App\Formatters\MyCustomFormatter::class,
    ],
];

$app['config']->set('app.env', 'testing');
$app['config']->set('app.debug', true);
$app['config']->set('api-response-kit.debug.show_trace', true);
$app['config']->set('api-response-kit.debug.hide_sql_errors', false);

public function test_exception_hides_details_in_production(): void
{
    $this->app['config']->set('api-response-kit.debug.show_trace', false);

    $data = $this->kit->exception(new \RuntimeException('secret'))->getData(true);

    $this->assertSame('An unexpected error occurred', $data['message']);
    $this->assertNull($data['errors']);
}
bash
php artisan vendor:publish --tag=api-response-kit-config
text
api-response-kit/
├── config/
│   └── api-response-kit.php
├── src/
│   ├── ApiResponseKitServiceProvider.php
│   ├── ApiResponseKit.php
│   ├── Facades/
│   │   └── ApiResponseKit.php
│   ├── Middleware/
│   │   └── ApiResponseKitMiddleware.php
│   ├── Formatters/
│   │   ├── SuccessFormatter.php
│   │   ├── ErrorFormatter.php
│   │   ├── ValidationFormatter.php
│   │   └── ExceptionFormatter.php
│   ├── Contracts/
│   │   └── ResponseFormatterInterface.php
│   └── Support/
│       ├── ResponseSchema.php
│       ├── RequestIdGenerator.php
│       └── ConfigResolver.php
├── tests/
│   ├── TestCase.php
│   ├── SuccessFormatterTest.php
│   ├── ErrorFormatterTest.php
│   └── MiddlewareTest.php
├── phpunit.xml
├── composer.json
└── README.md