kenny1911/symfony-http-exception
Composer 安装命令:
composer require kenny1911/symfony-http-exception
包简介
The library provides a convenient way to convert PHP exceptions into Symfony HTTP exceptions with support for status codes, messages, headers, and translation capabilities.
README 文档
README
[ English | Русский ]
The kenny1911/symfony-http-exception library provides a convenient way to convert PHP exceptions into Symfony HTTP exceptions with support for status codes, messages, headers, and translation capabilities.
Key Features
- Convert any exception to
HttpExceptionusing attributes - Support for HTTP status codes, messages, headers, and translation parameters
- Built-in aliases for common HTTP errors
- Symfony Translation component integration
- Dynamic values using Symfony ExpressionLanguage
Installation
Install via Composer:
composer require kenny1911/symfony-http-exception
Register the event subscriber in your Symfony configuration:
# config/services.yaml services: Kenny1911\SymfonyHttpException\ErrorListener: arguments: - '@translator' - !service { class: Symfony\Component\ExpressionLanguage\ExpressionLanguage } tags: - { name: kernel.event_subscriber }
Arguments
$translatorand$expressionLanguageis not required
As the third argument $metadataLoader, you can pass the instance of Kenny1911\SymfonyHttpException\Metadata\MetadataLoader.
The default value is AttributeMetadataLoader. Apart from this, there is also:
ArrayMetadataLoader- use map exception class toBaseHttpExceptioninstance.ConfigMetadataLoader- use associative array for describe configuration.ChainMetadataLoader- combine multiplyMetadataLoaderinstances.
Эти загрузчики могут использоваться для описания сторонних классов исключения.
Example of advanced configuration:
# config/services.yaml services: Kenny1911\SymfonyHttpException\ErrorListener: arguments: - '@translator' - !service { class: Symfony\Component\ExpressionLanguage\ExpressionLanguage } - !service class: Kenny1911\SymfonyHttpException\Metadata\ChainMetadataLoader arguments: - !service { class: Kenny1911\SymfonyHttpException\Metadata\ArrayMetadataLoader } - !service class: Kenny1911\SymfonyHttpException\Metadata\ConfigMetadataLoader arguments: - App\Exceptions\SomeException: statusCode: 401 message: 'Not authorized. Error code: { error_code }. { exception_message }' parameters: '{ error_code }': '10420' '{ exception_message }': 'expr(e.getMessage())' translationDomain: message headers: 'X-Error-Code': '10420' 'X-Error-Message': 'expr(e.getMessage())' App\Exceptions\OtherException: statusCode: 400 tags: - { name: kernel.event_subscriber }
Usage
Basic Example
use Kenny1911\SymfonyHttpException\Attribute\HttpException; use Symfony\Component\HttpFoundation\Response; #[HttpException(statusCode: Response::HTTP_FORBIDDEN)] final class UserIsBlockedException extends Exception {}
Advanced Example
use Kenny1911\SymfonyHttpException\Attribute\HttpException; use Kenny1911\SymfonyHttpException\ExpressionLanguage\Expression; use Symfony\Component\HttpFoundation\Response; #[HttpException( statusCode: Response::HTTP_FORBIDDEN, message: 'User { username } is blocked. Error code: { error_code }. Reason: { reason }.', parameters: [ '{ error_code }' => '1234', '{ username }' => new Expression('e.username'), '{ reason }' => new Expression('translator.trans(e.reason)'), ], translationDomain: 'message', headers: [ 'X-Error-Code' => '1024', 'X-Username' => new Expression('e.username'), 'X-Reason' => new Expression('translator.trans(e.reason)'), ], )] final class UserIsBlockedException extends Exception { public function __construct( public readonly string $username, public readonly string $reason, ) {} }
Built-in Aliases
use Kenny1911\SymfonyHttpException\Attribute\AccessDeniedHttpException; #[AccessDeniedHttpException] final class UserIsBlockedException extends Exception {}
use Kenny1911\SymfonyHttpException\Attribute\NotFoundHttpException; #[NotFoundHttpException] final class UserNotFoundException extends Exception {}
use Kenny1911\SymfonyHttpException\Attribute\BadRequestHttpException; #[BadRequestHttpException] final class InvalidUsernameException extends Exception {}
License
This library is released under the MIT License.
kenny1911/symfony-http-exception 适用场景与选型建议
kenny1911/symfony-http-exception 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.43k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「http」 「exception」 「code」 「message」 「convert」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kenny1911/symfony-http-exception 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kenny1911/symfony-http-exception 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kenny1911/symfony-http-exception 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
The bundle for easy using json-rpc api on your project
Bundle Symfony DaplosBundle
Error and Exception override and observers.
Error handler based on Booboo with HTML and JSON support
Standard PHP Library Extension featuring an ExceptionInterface.
统计信息
- 总下载量: 1.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-22