myschoolmanagement / bindto
将请求绑定到 DTO/命令的简单方法
2.0.7
2024-09-26 09:09 UTC
Requires
- php: >=7.2
- doctrine/cache: ^1.13
- doctrine/common: ^2.6|^3.0
- liuggio/filler-dto: ^0.1.0
- lstrojny/functional-php: ^1.17.0
- psr/http-message: ^1.0
- ramsey/uuid: ^3.0|^4.0
- symfony/validator: ^4.2|^5.0
- ursula/entity-framework: >=8.0.0
Requires (Dev)
- phpunit/phpunit: ~4.7|~5.0|~6.0|~7.0|~8.0
- symfony/http-foundation: ^3.0|^4.0|^5.0
Suggests
- symfony/property-access: Required to use ConvertingObjectMapper
- dev-master
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.0.1
- 1.0.0
- 0.4.11
- 0.4.10
- 0.4.9
- 0.4.8
- 0.4.7
- 0.4.6
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.1
- 0.3.0
- 0.2.0
- v0.1.0
- dev-dev/1.9-msm-12693
- dev-feature-autoconfiguration
- dev-MV-840
- dev-MV-734
- dev-feature-casting-object-mapper
- dev-feature-converters
- dev-bug-request-get-method
- dev-changed-member-accessibility
This package is auto-updated.
Last update: 2024-09-26 09:10:26 UTC
README
将请求绑定到 DTO/命令的最简单方法。
Bindto 帮助您使用 DTO 和命令与 API 和数据验证一起工作。
它非常快(不使用反射)并且将请求绑定到您的类。
这是停止使用 API 的缓慢且复杂的 Symfony Form 组件的最智能方式。
安装
composer require pugx/bindto
使用方法
示例,您需要创建一个 Post/Patch/Put Api
1. 创建一个简单的类作为请求的主体
提示:您可以使用 Symfony 验证组件的注解
use Symfony\Component\Validator\Constraints as Assert; Class CreateFeedback { /** * @Assert\NotBlank(groups={"POST"}) * @Assert\Type(type="string") */ public $subject; /** * @Assert\NotNull(groups={"POST"}) * @Assert\Type(type="string") * @Assert\Length( * min = 10, * max = 500) */ public $body; }
2. 在您的控制器中
如果您使用 Silex,您可能希望强制输入验证,请参考 http://silex.sensiolabs.org/doc/usage.html#example-post-route
require_once __DIR__.'/../vendor/autoload.php'; use Bindto\Binder; $app = new Silex\Application(); $app->post('/feedback', function (Request $request) { $binder = Binder::createSimpleProductionBinder(); $result = $binder->bind($request, CreateFeedback::class); if (!$result->isValid()) { throw new \Exception($result->getViolations()); } $createFeedBack = $result->getData(); mail($createFeedBack->subject, '[YourSite] Feedback', $createFeedBack->body); return new Response('Thank you for your feedback!', 201); }); $app->run();
PATCH 支持部分修改
如果您想部分 PATCH,请使用验证组
/** * @Assert\NotNull(groups={"POST"}) * @Assert\Type(type="string") * @Assert\Length( * min = 10, * max = 500) */ public $body;
对于 POST 请求,将使用所有断言,而对于 PUT 和 PATCH,只有 Type 和 Length 断言。
待办事项
- 递归绑定器?
- 集合?
- twig 辅助函数?
- 测试将 maptest 从 bindertest 解耦
- Silex 提供程序?
- sf 包?
运行测试
composer dump-autoload bin/phpunit