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


use APIResponseTrait;

use MA\LaravelApiResponse\Traits\APIResponseTrait;

class TestController extends Controller
{
    use APIResponseTrait;
    
    public function index()
    {
        return $this->apiResponse([
            'message' => 'Test Message',
            'data' => [
                [
                    'id' => 1,
                    'name' => 'Test Name',
                ],
                [
                    'id' => 2,
                    'name' => 'Test Name 2',
                ],
                [
                    'id' => 3,
                    'name' => 'Test Name 3',
                ],
            ]
        ]);
    }
}

return $this->apiOk("test message");

// Or

return $this->apiResponse("test message");

return $this->apiOk([
    [
        'id' => 1,
        'name' => 'Test Name',
    ],
    [
        'id' => 2,
        'name' => 'Test Name 2',
    ],
    [
        'id' => 3,
        'name' => 'Test Name 3',
    ],
]);

// Or

return $this->apiResponse([
    [
        'id' => 1,
        'name' => 'Test Name',
    ],
    [
        'id' => 2,
        'name' => 'Test Name 2',
    ],
    [
        'id' => 3,
        'name' => 'Test Name 3',
    ],
]);

return $this->apiResponse([
    'type' => 'notfound', // 'type' => 404,
]);

/*
* Error code examples:
* THROTTLING_ERROR
* 1021
* ErrorCodesEnum::THROTTLING_ERROR
* ErrorCodesEnum::THROTTLING_ERROR->name
* ErrorCodesEnum::THROTTLING_ERROR->value
*/
return $this->apiResponse([
    'type' => 'notfound',
    'filter_data' => true,
    'throw_exception' => true,
    'message' => 'TestMessage',
    'errorCode' => 'INVALID_CREDENTIALS', // you can make it string, integer or UnitEnum
]);

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

$tests = Tests::query()
        ->where('is_active', true)
        ->paginate(2);
        
return $this->apiPaginateResponse($tests);

$this->apiPaginate($pagination, bool $reverse_data = false)
 

$this->apiException($errors = null, bool $throw_exception = true, $errorCode = null)
$this->apiNotFound($errors = null, bool $throw_exception = true, $errorCode = null)
$this->apiBadRequest($errors = null, bool $throw_exception = true, $errorCode = null)
 

return $this->apiForbidden('TEST MESSAGE', [
            'error_1' => 'asdasasdasd',
            'error_2' => 'asdasdasdasd'
        ], 'FORBIDDEN');

return $this->apiUnauthenticated('TEST MESSAGE', [
            'error_1' => 'asdasasdasd',
            'error_2' => 'asdasdasdasd'
        ], 'UNAUTHORIZED_ACCESS');

$this->apiValidate($data, $roles, array $messages = [], array $customAttributes = [])
 

return $this->apiDD([
    [
        'id' => 1,
        'name' => 'Test Name',
    ],
    [
        'id' => 2,
        'name' => 'Test Name 2',
    ],
    [
        'id' => 3,
        'name' => 'Test Name 3',
    ],
]);
cmd
php artisan vendor:publish --provider="MA\LaravelApiResponse\Providers\APIResponseProvider" --tag="lapi-response"
cmd
php artisan lapi-response:publish-error-codes
cmd
php artisan lapi-response:publish-error-codes CustomErrorCodesEnum