1. Go to this page and download the library: Download rasuvaeff/yii3-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/ */
rasuvaeff / yii3-api-problem example snippets
use Nyholm\Psr7\Factory\Psr17Factory;
use Rasuvaeff\Yii3ApiProblem\ProblemDetails;
use Rasuvaeff\Yii3ApiProblem\ProblemDetailsResponseFactory;
$problem = ProblemDetails::create(
type: 'https://example.com/problems/insufficient-funds',
title: 'Insufficient funds',
status: 403,
detail: 'The account balance is too low.',
instance: '/transfers/42',
);
$psr17 = new Psr17Factory();
$response = (new ProblemDetailsResponseFactory($psr17, $psr17))
->toResponse($problem);
use Rasuvaeff\Yii3ApiProblem\InvalidParam;
$problem = ProblemDetails::fromStatus(status: 422)->withInvalidParams(
InvalidParam::create(name: 'email', reason: 'Invalid email address'),
InvalidParam::create(name: 'age', reason: 'Must be at least 18'),
);
use Rasuvaeff\Yii3ApiProblem\ProblemDetails;
use Rasuvaeff\Yii3ApiProblem\ProblemDetailsException;
throw ProblemDetailsException::forProblem(
details: ProblemDetails::create(
title: 'Validation failed',
status: 422,
),
headers: ['Retry-After' => '60'],
);
use Nyholm\Psr7\Factory\Psr17Factory;
use Rasuvaeff\Yii3ApiProblem\DefaultExceptionMapper;
use Rasuvaeff\Yii3ApiProblem\ProblemDetailsMiddleware;
use Rasuvaeff\Yii3ApiProblem\ProblemDetailsResponseFactory;
$psr17 = new Psr17Factory();
$middleware = new ProblemDetailsMiddleware(
responseFactory: new ProblemDetailsResponseFactory($psr17, $psr17),
exceptionMapper: new DefaultExceptionMapper(),
debug: false,
);
use Psr\Http\Message\ServerRequestInterface;
use Rasuvaeff\Yii3ApiProblem\ThrowableReporterInterface;
final readonly class SentryThrowableReporter implements ThrowableReporterInterface
{
public function report(Throwable $throwable, ServerRequestInterface $request): void
{
Sentry\captureException($throwable);
}
}
$middleware = new ProblemDetailsMiddleware(
responseFactory: $responseFactory,
exceptionMapper: $mapper,
throwableReporter: new SentryThrowableReporter(),
);
use Rasuvaeff\Yii3ApiProblem\CorrelationIdProvider;
final readonly class ApiProblemCorrelationIdProvider implements CorrelationIdProvider
{
public function __construct(
private Rasuvaeff\Yii3CorrelationId\CorrelationIdProvider $provider,
) {}
public function getCorrelationId(): ?string
{
return $this->provider->tryGet();
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.