timezhero/laravel-rfc9457 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

timezhero/laravel-rfc9457

Composer 安装命令:

composer require timezhero/laravel-rfc9457

包简介

RFC 9457 Problem Details for Laravel exceptions

README 文档

README

A Laravel package that standardizes API error responses using the RFC 9457 Problem Details specification. Exceptions opt in via a trait and PHP attributes — no base class required, no HTTP coupling on your domain exceptions.

Installation

composer require timezhero/laravel-rfc9457

The service provider is auto-discovered.

Quick start

1. Register the handler in bootstrap/app.php:

use TimeZHero\Rfc9457\ProblemDetailsHandler;

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

2. Scaffold an exception:

php artisan make:problem-exception InsufficientStock

3. Throw it:

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

Response (409, application/problem+json):

{
    "type": "/problems/insufficient-stock",
    "title": "Insufficient Stock",
    "detail": "The requested quantity of 30 exceeds the available stock of 5.",
    "status": 409,
    "instance": "/api/orders",
    "product_id": 42,
    "requested": 30,
    "available": 5
}

Validation errors, HTTP exceptions, and generic 5xx are also wrapped automatically.

CLI

Command What it does
php artisan make:problem-exception {name} Creates exception class + translation entry + documentation view
php artisan vendor:publish --tag=rfc9457-config Publish config
php artisan vendor:publish --tag=rfc9457-views Publish views to customize the docs layout

Advanced

Manual exception

#[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,
        ]);
    }
}
  • #[Problem] — title + HTTP status. Type URI is derived from class name. Omit to auto-derive everything (defaults to 500)
  • #[Extension] — marks properties as extension fields (snake_cased in output). Use name: to override the key
  • detail() — user-facing message (separate from internal $message). Return '' to omit
  • extensions() — override to add computed fields at runtime. Merged with attribute extensions

Translations

Convention-based in lang/{locale}/exceptions.php:

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

Falls back to the raw #[Problem] title if no translation exists.

Documentation pages

Each exception gets a page at /problems/{slug}. Add a description view at resources/views/vendor/rfc9457/details/{slug}.blade.php. Extensions and metadata are rendered automatically.

Configuration

// config/rfc9457.php
return [
    'exception_namespace' => 'App\\Exceptions',
    'documentation' => [
        'enabled' => true,       // set false to disable route + views entirely
        'route_prefix' => '/problems',
    ],
];

timezhero/laravel-rfc9457 适用场景与选型建议

timezhero/laravel-rfc9457 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 04 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「exceptions」 「api」 「laravel」 「problem-details」 「rfc9457」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 timezhero/laravel-rfc9457 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 timezhero/laravel-rfc9457 我们能提供哪些服务?
定制开发 / 二次开发

基于 timezhero/laravel-rfc9457 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 41
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2026-04-30