PHP code example of codebot / laravel-response-builder
1. Go to this page and download the library: Download codebot/laravel-response-builder 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/ */
codebot / laravel-response-builder example snippets
use \Rb\Core\RbValidator;
use Illuminate\Support\Facades\Validator;
$validator = new Validator($data, $rules); // assuming you have a $validator instance
RbValidator::validate($validator); // throws HttpResponseException or returns boolean true
use \Rb\Core\Response;
use \Rb\Core\HttpStatusCode;
$response = new Response();
$response->setStatusCode(HttpStatusCode::OK); // will be returned if no data set
$response->setErrors($errors); // null will be returned if no error set
$response->getArray(); // will return an array of data set
$response->getResponse(); // will return a json response using Laravel's response() helper
use \Rb\Core\Response;
use \Rb\Core\HttpStatusCode;
$response = new Response();
$response->setStatusCode(HttpStatusCode::OK)
->setMessage('Some inspiring message.');
// ...
use \Rb\Core\HttpStatusCode;
HttpStatusCode::OK; // returns status code 200 (integer)
HttpStatusCode::getMessageByCode(HttpStatusCode::NOT_FOUND); // returns string "Not Found"
HttpStatusCode::getCodeWithMessage(HttpStatusCode::CREATED); // returns string - code and message, e.g. "201 Created"
use \Rb\Facade\Response;
use \Rb\Core\HttpStatusCode;
return Response::success(
data: $data,
message: 'Created List of users.'
);
return Response::error(
errors: $errors,
message: 'Invalid input.',
statusCode: HttpStatusCode::UNPROCESSABLE_ENTITY
);
// Without $data (the $errors parameter is also optional)
return Response::success(message: 'User deleted.');