PHP code example of aliziodev / laravel-api-response

1. Go to this page and download the library: Download aliziodev/laravel-api-response 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/ */

    

aliziodev / laravel-api-response example snippets


use Aliziodev\ApiResponse\Facades\ApiResponse;

// Success Response
return ApiResponse::success(
    data: ['user' => $user],
    message: 'User retrieved successfully',
    meta: ['total' => 1]
);

// Error Response
return ApiResponse::error(
    message: 'Something went wrong',
    errors: ['database' => 'Connection failed'],
    code: 500
);

// Fail Response
return ApiResponse::fail(
    message: 'Validation failed',
    errors: ['email' => ['Email is 



use Aliziodev\ApiResponse\Exceptions\ApiExceptionHandler;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Http\Request;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__ . '/../routes/web.php',
        api: __DIR__ . '/../routes/api.php',
        commands: __DIR__ . '/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        // Add your custom middleware here
    })
    ->withExceptions(function (Exceptions $exceptions) {
        $exceptions->renderable(function (\Throwable $e, Request $request) {
            if ($request->expectsJson()) {
                return app(ApiExceptionHandler::class)->handle($e, $request);
            }
        });
    })->create();

// Basic Success (200)
ApiResponse::success(
    data: $data,
    message: 'Success',
    meta: ['page' => 1]
);

// Created (201)
ApiResponse::created(
    data: $newResource,
    message: 'Resource created successfully'
);

// Accepted (202)
ApiResponse::accepted(
    data: ['job_id' => 'abc123'],
    message: 'Job queued'
);

// No Content (204)
ApiResponse::noContent(
    message: 'Resource deleted'
);

// Custom Success
ApiResponse::success(
    data: $data,
    message: 'Custom success message',
    meta: ['custom' => 'meta'],
    code: 200
);

// Server Error (500)
ApiResponse::error(
    message: 'Server error occurred',
    errors: ['server' => 'Internal error']
);

// Service Unavailable (503)
ApiResponse::serviceUnavailable(
    message: 'Service is down',
    errors: ['maintenance' => 'Scheduled maintenance']
);

// Maintenance Mode (503)
ApiResponse::maintenance(
    message: 'System maintenance',
    errors: ['status' => 'Please try again later']
);

// Bad Request (400)
ApiResponse::fail(
    message: 'Invalid input',
    errors: ['field' => 'Invalid value']
);

// Unauthorized (401)
ApiResponse::unauthorized(
    message: 'Authentication 04)
ApiResponse::notFound(
    message: 'Resource not found',
    errors: ['id' => 'Record does not exist']
);

// Validation Error (422)
ApiResponse::validationError(
    errors: ['email' => ['Email is 

// These keys will be masked in logs
private static array $sensitiveKeys = [
    'password',
    'secret',
    'token',
    'authorization',
    'cookie',
    'api_key',
    'key',
    'private',
    'credential',
];
bash
composer analyse