PHP code example of imran / laravel-api-toolkit

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

    

imran / laravel-api-toolkit example snippets


return ApiResponse::success(User::paginate(15));
// or with a resource: return ApiResponse::success(UserResource::collection(User::paginate(15)));

use Imran\ApiToolkit\Facades\ApiResponse;

return ApiResponse::success($user);
return ApiResponse::success($user, 'User fetched.', 200, ['total' => 1]);
return ApiResponse::created($user);
return ApiResponse::deleted();
return ApiResponse::noContent();
return ApiResponse::loading('Export queued.', ['job_id' => $job->id]);

return ApiResponse::error('Something went wrong.', 400);
return ApiResponse::unauthorized();
return ApiResponse::forbidden();
return ApiResponse::notFound('User not found.');
return ApiResponse::validationError($validator->errors());

use Imran\ApiToolkit\Traits\ApiResponser;

class UserController extends Controller
{
    use ApiResponser;

    public function show(User $user)
    {
        return $this->success($user);
    }

    public function store(StoreUserRequest $request)
    {
        $user = User::create($request->validated());

        return $this->created($user);
    }
}

class Controller extends BaseController
{
    use ApiResponser {
        success as protected apiSuccess;
        error as protected apiError;
    }

    // your own success()/error() methods keep working; call apiSuccess()/apiError() for this package's versions
}

return api_success($user);
return api_error('Invalid request.', 400);

use Imran\ApiToolkit\Exceptions\ApiException;

throw ApiException::notFound('Order not found.');
throw ApiException::forbidden();
throw ApiException::conflict('Email already registered.');
throw ApiException::validation(['email' => ['Already taken.']]);
throw ApiException::tooManyRequests('Slow down.', retryAfterSeconds: 30);
throw ApiException::badRequest('Malformed payload.');

// Or fully custom:
throw ApiException::make('Upstream provider timed out.', 502, 'UPSTREAM_TIMEOUT');

'exception_map' => [
    \App\Exceptions\InsufficientBalanceException::class => [
        'status' => 422,
        'error_code' => 'INSUFFICIENT_BALANCE',
        'message' => 'The account balance is too low for this operation.',
    ],
],

class InsufficientBalanceException extends \Exception
{
    public function errors(): array
    {
        return ['balance' => ['The account balance is too low for this operation.']];
    }
}

// config/api-toolkit.php
'auto_register' => false,

use Imran\ApiToolkit\Exceptions\ExceptionRenderer;

$renderer = app(ExceptionRenderer::class);

if ($renderer->shouldHandle($request)) {
    return $renderer->render($e, $request);
}
bash
php artisan vendor:publish --tag=api-toolkit-config
php artisan vendor:publish --tag=api-toolkit-lang