PHP code example of befuture / api-response

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

    

befuture / api-response example snippets


// config/app.php
'providers' => [
    BeFuture\ApiResponse\ApiResponseServiceProvider::class,
],

'aliases' => [
    'ApiResponse' => BeFuture\ApiResponse\Facades\ApiResponse::class,
],

use BeFuture\ApiResponse\ApiResponse;

// Success response
return ApiResponse::success(
    data: ['id' => 1],
    message: 'Success'
);

// Error response
return ApiResponse::error(
    message: 'An error occurred.',
    code: 'UNEXPECTED_ERROR',
    httpStatus: 500
);

// Validation error
return ApiResponse::validationError($validator->errors()->toArray());

use BeFuture\ApiResponse\ApiResponseFactory;

$factory = new ApiResponseFactory();

$responseArray = $factory->success(
    data: ['foo' => 'bar'],
    message: 'OK'
);

header('Content-Type: application/json');
http_response_code($responseArray['_status']);
echo json_encode($responseArray['body']);