PHP code example of timezhero / laravel-rfc9457

1. Go to this page and download the library: Download timezhero/laravel-rfc9457 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/ */

    

timezhero / laravel-rfc9457 example snippets


use TimeZHero\Rfc9457\ProblemDetailsHandler;

return Application::configure(basePath: dirname(__DIR__))
    ->withExceptions(function (Exceptions $exceptions): void {
        ProblemDetailsHandler::register($exceptions);
    })
    ->create();

throw new InsufficientStockException(productId: 42, requested: 30, available: 5);

#[Problem('Rate Limit Exceeded', Response::HTTP_TOO_MANY_REQUESTS)]
class RateLimitExceededException extends \RuntimeException
{
    use ProblemRfc9457;

    public function __construct(
        #[Extension(description: 'Seconds to wait before retrying.')]
        private readonly int $retryAfterSeconds,
    ) {
        parent::__construct('Internal log message');
    }

    protected function detail(): string
    {
        return __('exceptions.rate_limit_exceeded.detail', [
            'retry_after_seconds' => $this->retryAfterSeconds,
        ]);
    }
}

'insufficient_stock' => [
    'title' => 'Insufficient Stock',
    'detail' => 'Requested :requested but only :available available.',
],

// config/rfc9457.php
return [
    'exception_namespace' => 'App\\Exceptions',
    'documentation' => [
        'enabled' => true,       // set false to disable route + views entirely
        'route_prefix' => '/problems',
    ],
];
bash
php artisan make:problem-exception InsufficientStock