dimkinthepro/http-bundle
最新稳定版本:1.0.0
Composer 安装命令:
composer require dimkinthepro/http-bundle
包简介
Request validation bundle for Symfony
README 文档
README
1. Installation:
composer require dimkinthepro/http-bundle
2. Check bundles config:
# config/bundles.php return [ #... Dimkinthepro\Http\DimkintheproHttpBundle::class => ['all' => true], ];
3. Create bundle configuration:
# config/packages/dimkinthepro_http.yaml dimkinthepro_http: request_validation_enabled: true extra_fields_allowed: true handle_validation_errors: true response_error_format: json
4. Check validator config:
# config/packages/validator.yaml framework: #... validation: #... mapping: paths: #... - '%kernel.project_dir%/config/validator/'
5. Create a controller DTO:
# src/App/DTO/RequestDTO.php namespace App\DTO; use Dimkinthepro\Http\Domain\DTO\ValidatedDTOInterface; class RequestDTO implements ValidatedDTOInterface { public string $method; public int $parameter; public \DateTimeImmutable $date; public App\Enum\FooEnum $enum; }
6. Create a validation config for DTO:
# config/validator/RequestDTO.yaml App\DTO\RequestDTO: properties: method: - NotBlank: ~ parameter: - NotBlank: ~ - GreaterThan: 0 - LessThanOrEqual: 255 date: - NotBlank: ~ enum: - NotBlank: ~
7. Use validated DTO in your controller:
# src/App/Controller/Controller.php namespace App\Controller; use App\DTO\RequestDTO; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; class Controller extends AbstractController { public function __invoke(RequestDTO $DTO): JsonResponse { return new JsonResponse([ 'method' => $DTO->method, 'parameter' => $DTO->parameter, 'date' => $DTO->date, 'enum' => $DTO->enum->value, ]); } }
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-09-10