1. Go to this page and download the library: Download phuoc/laravel-exception 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/ */
phuoc / laravel-exception example snippets
// Has to be in prioritized order, e.g. highest priority first.
'map' => [
AuthenticationException::class => CustomException\AuthenticationException::class,
AuthorizationException::class => CustomException\AuthorizationException::class,
ValidationException::class => CustomException\ValidationException::class,
Exception::class => CustomException\Exception::class,
],
namespace App\Exceptions\YourExceptions;
use Phuocnt\LaravelException\Exceptions\CustomException as BaseCustomException;
use Symfony\Component\HttpFoundation\Response;
class UnprocessableEntityHttpException extends BaseCustomException
{
protected $statusCode = Response::HTTP_UNPROCESSABLE_ENTITY;
public function __construct(\Exception $e)
{
parent::__construct($e);
}
pubic function render($request, $data = null) {
$data = [
'firstLine' => 'firstLine',
'secondLine' => 'secondLine',
];
return response()->json()->setStatusCode($this->statusCode)->setData($data);
}
}
// Has to be in prioritized order, e.g. highest priority first.
'map' => [
\Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException::class
=> App\Exceptions\YourExceptions\UnprocessableEntityHttpException::class,
AuthenticationException::class => CustomException\AuthenticationException::class,
AuthorizationException::class => CustomException\AuthorizationException::class,
ValidationException::class => CustomException\ValidationException::class,
Exception::class => CustomException\Exception::class,
],
namespace App\Exceptions;
class SomeException extends \Exception
{
public $statusCode = 400;
public function __construct($message = "Oops, exception's thrown", $code = 99999)
{
parent::__construct(
$message,
$code
);
}
}