PHP code example of farshidrezaei / vandar-responder
1. Go to this page and download the library: Download farshidrezaei/vandar-responder 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/ */
farshidrezaei / vandar-responder example snippets
config('responder.errors.YOUR_ERROR')
config('responder.errors.INTERNAL_ERROR')
config('responder.errors.EXTERNAL_SERVICE_ERROR')
__('responder::exceptions.YOUR_ERROR')
__('responder::exceptions.validation')
Responder::success(?string $message = null, mixed $data = null): Illuminate\Http\JsonResponse
Responder::successResourceCollection(null|string $message = null, Illuminate\Http\Resources\Json\AnonymousResourceCollection $data) :lluminate\Http\Resources\Json\AnonymousResourceCollection
Responder::failure(int $errorCode, string $stringErrorCode, null|string $message = null, array|null $errors = [], array|null $data = []): Illuminate\Http\JsonResponse
// php 8
return response()->failure(
errorCode: Response::HTTP_INTERNAL_SERVER_ERROR,// 500
stringErrorCode: config('responder.errors.INTERNAL_ERROR'),
message: "Service isn't available now. try again later.",
data:['foo'=>'bar']
);
use FarshidRezaei\VandarResponder\Services\Responder;
// php 8
return Responder::failure(
errorCode: Response::HTTP_INTERNAL_SERVER_ERROR,// 500
stringErrorCode: config('responder.errors.INTERNAL_ERROR'),
message: "Service isn't available now. try again later.",
data:['foo'=>'bar']
);
// php 7.4
return Responder::failure(
Response::HTTP_INTERNAL_SERVER_ERROR,// 500
config('responder.errors.INTERNAL_ERROR'),
"Service isn't available now. try again later.",
['foo'=>'bar']
);
// php 8
return responder()->success(
message:"User Info",
data: [
"name"=>"Farshid",
"company"=>"Vandar",
"Position"=>"Full-Stack Web Developer"
]
);
// php 8
$users=User::paginate();
return Responder::successResourceCollection(
message: 'User List.',
data: USerResource::collection($users)
);
public function render($request, Throwable $e): \Illuminate\Http\Response|JsonResponse|Response
{
//...
if ($request->expectsJson() && $exceptionResponse = ApiExceptionHandler::handle($e)) {
return $exceptionResponse;
}
//...
return parent::render($request, $e);
}
'customExceptions' => [
RuntimeException::class => CustomDefaultException::class,
Exception::class => CustomDefaultException::class,
ValidationException::class => CustomValidationException::class,
NotFoundHttpException::class => CustomNotFoundHttpException::class,
MethodNotAllowedException::class => CustomMethodNotAllowed::class,
ModelNotFoundException::class => CustomNotFoundHttpException::class,
AuthenticationException::class => CustomAuthenticationException::class,
AuthorizationException::class => CustomUnauthorizedException::class,
UnauthorizedHttpException::class => CustomUnauthorizedException::class,
ThrottleRequestsException::class => CustomThrottleRequestsException::class
]
class CustomDefaultException extends AbstractApiCustomException implements ApiCustomExceptionInterface
{
public function __construct(?Exception $exception)
{
$this->errorCode = Response::HTTP_INTERNAL_SERVER_ERROR; // exception Status Code
$this->stringErrorCode = config('responder.errors.EXTERNAL_SERVICE_ERROR'); // string error code
$this->errorMessage = __('responder::exceptions.generalServerError'); // message of error
parent::__construct();
}
}
bash
#customExceptions
php artisan vendor:publish --provider="FarshidRezaei\VandarResponder\Providers\ResponderServiceProvider" --tag="customExceptions"