guhemama/http-precondition-bundle
Composer 安装命令:
composer require guhemama/http-precondition-bundle
包简介
Introduces route preconditions when using the Symfony HttpKernel component.
关键字:
README 文档
README
HTTP Precondition Bundle
This bundle introduces a Precondition attribute that can be used
to check for certain conditions when routing. When the conditions are
not met, an exception is thrown (412 Precondition failed).
Installation
Install the bundle with Composer:
$ composer require guhemama/http-precondition-bundle
Usage
To define a new precondition, import the Guhemama\HttpPreconditionBundle\Annotations\Precondition
attribute and provide an expression expr to be evaluated - any valid ExpressionLanguage expression is accepted.
<?php namespace App\Controller; use Guhemama\HttpPreconditionBundle\Annotations\Precondition; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Annotation\Route; class QuestionController extends AbstractController { #[Precondition(expr: "1+1 > 2")] #[Route('/question')] public function index(): JsonResponse { return $this->json(['answer' => 42]); } }
When using the ParamConverter (Symfony 5) or the MapEntity (Symfony 6+) attributes,
you can also refer to the mapped entities in the precondition expression:
<?php use App\Entity\Question; use Guhemama\HttpPreconditionBundle\Annotations\Precondition; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Bridge\Doctrine\Attribute\MapEntity; class QuestionController extends AbstractController { #[Precondition(expr: '!question.isAnswered()', message: 'Cannot answer an already answered question.', payload: ['error' => 'QUESTION_ALREADY_ANSWERED'])] #[Route(path: '/question/{id}', methods: ['POST'])] public function update( #[MapEntity(Question::class)] Question $question ): Response { return new JsonResponse($question); } }
When the precondition expression evaluates to false, an \Guhemama\HttpPreconditionBundle\Exception\Http\PreconditionFailedHttpException exception is thrown.
This exception also includes an instance of the Precondition should you need access to its configured values (e.g. payload).
Configuration
This bundle depends on the ExpressionLanguage component.
If you have extended the expression language or would like to use
a another instance of it instead of the default one, update the configuration as follows, replacing
my_custom_expression_lang_service with your service name:
# config/packages/guhemama_http_precondition.yaml guhemama_http_precondition: expression_language: my_custom_expression_lang_service
guhemama/http-precondition-bundle 适用场景与选型建议
guhemama/http-precondition-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.11k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 12 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「http」 「precondition」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 guhemama/http-precondition-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 guhemama/http-precondition-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 guhemama/http-precondition-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides runtime assertions
repository php library
The bundle for easy using json-rpc api on your project
Guard clause assertion library to enforce parameter preconditions
Bundle Symfony DaplosBundle
http客户端
统计信息
- 总下载量: 3.11k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-23