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 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',
],
],
'extra' => [
'field1' => 'Field 1',
'field2' => 'Field 2'
],
]);
}
}
return $this->apiOk("test message");
// Or alternatively
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 alternatively
return $this->apiResponse([
[
'id' => 1,
'name' => 'Test Name',
],
[
'id' => 2,
'name' => 'Test Name 2',
],
[
'id' => 3,
'name' => 'Test Name 3',
],
]);
// Return api stream response
public function index()
{
// Message and status code are optional
return $this->apiStreamResponse($this->yieldUsers(), "Test Message", 200);
}
protected function yieldUsers(): Generator
{
foreach (User::query()->cursor() as $user) {
yield $user;
}
}
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', // can be 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 = [])
use APIRequestValidator;
return $this->apiDD([
[
'id' => 1,
'name' => 'Test Name',
],
[
'id' => 2,
'name' => 'Test Name 2',
],
[
'id' => 3,
'name' => 'Test Name 3',
],
]);
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