PHP code example of i3rror / lapi-response

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

    

i3rror / lapi-response example snippets


MA\LaravelApiResponse\Providers\APIResponseProvider::class

use MA\LaravelApiResponse\Traits\APIResponseTrait;

class YourController extends Controller
{
    use APIResponseTrait;

    // Your controller methods...
}

// In any file in your application
return apiOk($data, $message);
return apiNotFound(["Resource not found"], "Resource not found");
return apiResponse(['message' => 'Success', 'data' => $data]);

return $this->apiResponse([
    'message' => 'Success Message',
    'data' => $yourData,
    'extra' => [
        'field1' => 'Value 1',
        'field2' => 'Value 2'
    ],
]);

return $this->apiOk("Operation completed successfully", "Message");

return $this->apiOk($yourDataArray, "Message");

return $this->apiStreamResponse($this->yieldData(), "Streaming data", 200);

protected function yieldData(): Generator
{
    foreach (User::cursor() as $user) {
        yield $user;
    }
}

return $this->apiNotFound("Resource not found", "Error Not Found Message");

return $this->apiBadRequest("Invalid parameters");

return $this->apiResponse([
    'type' => 'notfound',
    'message' => 'Resource not found',
    'errorCode' => 'RESOURCE_NOT_FOUND',
]);

$data = $this->apiValidate($request, [
    'email' => ['

use MA\LaravelApiResponse\Traits\APIRequestValidator;

class UpdateAccountRequest extends FormRequest
{
    use APIRequestValidator;
    
    // If you want to set a custom message in the return response upon failing
    // You can use this
    protected ?string $errorMessage = 'Validation failed.',
}

$users = User::paginate(10);
return $this->apiPaginate($users);

// As trait methods
$this->apiResponse($data = null)

// As helper functions
apiResponse($data = null)

return $this->apiResponse([
        'status_code' => Response::HTTP_OK,
        'message' => 'This is custom message',
        'data' => [
            'data1' => 'custom data 1',
            'data2' => 'custom data 2'
        ],
        'extra' => [
            'extra1' => 'extra data 1',
            'extra2' => 'extra data 2'
        ]
    ]);

// As trait methods
$this->apiOk($data = null)
$this->apiResponse($data = null)

// As helper functions
apiOk($data = null)
apiResponse($data = null)

// As trait methods
$this->apiNotFound($errors = null, $throw_exception = true, $errorCode = null, $headers = [])
$this->apiBadRequest($errors = null, $throw_exception = true, $errorCode = null, $headers = [])
$this->apiException($errors = null, $throw_exception = true, $errorCode = null, $headers = [])
$this->apiUnauthenticated($message = null, $errors = null, $errorCode = null, $headers = [])
$this->apiForbidden($message = null, $errors = null, $errorCode = null, $headers = [])

// As helper functions
apiNotFound($errors = null, $throw_exception = true, $errorCode = null, $headers = [])
apiBadRequest($errors = null, $throw_exception = true, $errorCode = null, $headers = [])
apiException($errors = null, $throw_exception = true, $errorCode = null, $headers = [])
apiUnauthenticated($message = null, $errors = null, $errorCode = null, $headers = [])
apiForbidden($message = null, $errors = null, $errorCode = null, $headers = [])

// As trait methods
$this->apiPaginate($pagination, $appends = [], $reverse_data = false, $headers = [])
$this->apiValidate($data, $rules, $messages = [], $attributes = [])

// As helper functions
apiPaginate($pagination, $appends = [], $reverse_data = false, $headers = [])
apiValidate($data, $rules, $messages = [], $attributes = [])

// As trait methods
$this->apiDD($data) // Debug helper
$this->apiStreamResponse($generator, $message = null, $statusCode = 200, $headers = [])

// As helper functions
apiDD($data) // Debug helper
apiStreamResponse($generator, $message = null, $statusCode = 200, $headers = [])

// Remove null values from arrays
'removeNullDataValues' => false,

// Set empty data to null
'setNullEmptyData' => true,

// Format of validation errors
'returnValidationErrorsKeys' => true,

// Enable error codes
'enableErrorCodes' => true,

// Error codes enum class
'errorCodes' => \MA\LaravelApiResponse\Enums\ErrorCodesEnum::class,

// Error codes output format (string or integer)
'errorCodesType' => 'string',

// Return default error codes if not specified
'returnDefaultErrorCodes' => true,
bash
php artisan vendor:publish --provider="MA\LaravelApiResponse\Providers\APIResponseProvider" --tag="lapi-response-config"
bash
php artisan lapi-response:publish-error-codes
bash
php artisan lapi-response:publish-error-codes CustomErrorCodesEnum