PHP code example of pedrosalpr / laravel-api-problem
1. Go to this page and download the library: Download pedrosalpr/laravel-api-problem 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/ */
pedrosalpr / laravel-api-problem example snippets
use Pedrosalpr\LaravelApiProblem\LaravelApiProblem;
public function register(): void
{
...
$this->renderable(function (\Throwable $e, Request $request) {
if ($request->is('api/*') || $this->shouldReturnJson($request, $e)) {
$apiProblem = new LaravelApiProblem($e, $request);
return $apiProblem->render();
}
});
}
use Pedrosalpr\LaravelApiProblem\LaravelApiProblem;
...
->withExceptions(function (Exceptions $exceptions) {
$exceptions->render(function (\Throwable $e, Request $request) {
if ($request->is('api/*') || $this->shouldReturnJson($request, $e)) {
$apiProblem = new LaravelApiProblem($e, $request);
return $apiProblem->render();
}
});
})...
namespace App\Exceptions\ApiProblem;
use Pedrosalpr\LaravelApiProblem\Exceptions\LaravelApiProblemException;
class DummyException extends LaravelApiProblemException
{
}
namespace App\ApiProblem;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Pedrosalpr\LaravelApiProblem\Http\LaravelHttpApiProblem;
use Pedrosalpr\LaravelApiProblem\LaravelApiProblem;
class DummyApiProblem extends LaravelApiProblem
{
public function __construct(
protected \Throwable $exception,
protected Request $request
) {
match (get_class($exception)) {
\Exception::class => $this->dummy(),
default => parent::__construct($exception, $request)
};
}
protected function dummy()
{
$extensions = [
'errors' => "Dummy",
];
$this->apiProblem = new LaravelHttpApiProblem(
Response::HTTP_I_AM_A_TEAPOT,
$this->exception->getMessage(),
$this->getUriInstance(),
$extensions
);
}
}