relief_applications/symfony-request-validator-bundle
Composer 安装命令:
composer require relief_applications/symfony-request-validator-bundle
包简介
README 文档
README
Step 1: Download the Bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require relief_applications/symfony-request-validator-bundle @dev
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Step 2: Enable the Bundle
Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php file of your project:
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new RA\RequestValidatorBundle\RARequestValidatorBundle(), ); } }
Step 3: Configuration
# app/config/services.yml according to your project path imports: - { resource: "@RARequestValidatorBundle/Resources/config/services.yml" }
Step 4: Default routing
To test our default controller
# app/config/routing.yml according to your project path RequestValidatorBundle: resource: "@RARequestValidatorBundle/Resources/config/routing.yml"
Step 5: Usage
1. Define a custom constraints class
this class should extend RA\RequestValidatorBundle\RequestValidator\Constraints. The list of constraints is available here https://symfony.com/doc/current/reference/constraints.html
<?php namespace RequestValidatorBundle\Custom; use RA\RequestValidatorBundle\RequestValidator\Constraints as RequestValidatorConstraints; use Symfony\Component\Validator\Constraints\Choice; use Symfony\Component\Validator\Constraints\Collection; use Symfony\Component\Validator\Constraints\GreaterThan; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\NotEqualTo; use Symfony\Component\Validator\Constraints\Optional; use Symfony\Component\Validator\Constraints\Type; /** * TopicsConstraints * * A special class for topic constraints */ class TopicsConstraints extends RequestValidatorConstraints { protected function configure() : array { //Reusable rules can be store into variables starting by 'common' $commonCategoryId = new Optional ([ new Type('numeric'), new GreaterThan(0) ]); $commonNotBlank = new NotBlank(); return [ 'topics' => [ // rules for URI fields 'query' => new Collection([ 'method' => new Optional ([new Choice(['by'])]), 'category_id' => $commonCategoryId ]), // rules for POSTED fields 'request' => new Collection([ 'category_id' => $commonCategoryId, 'title' => $commonNotBlank, 'description' => $commonNotBlank, 'message' => $commonNotBlank ]), ], // rules for ANY fields 'topics2' => new Collection([ 'category_id' => $commonCategoryId, 'title' => $commonNotBlank, 'description' => $commonNotBlank, 'message' => $commonNotBlank ]), ]; } }
2. Using annotations
<?php //... use RequestValidatorBundle\Custom\TopicsConstraints; //our custom constraints class use RA\RequestValidatorBundle\Annotations\ValidateQuery; use RA\RequestValidatorBundle\Annotations\ValidateRequest; class TopicController extends Controller { /** * @Get("/", name="get_all_topics") * @ValidateQuery( configuration="topics", constraintsClass=TopicsConstraints::class) * Get a table of all the topics */ public function getAllAction(Request $request){ $categoryId = $request->get('category_id'); //... } /** * @Post("/", name="create_topic") * @ValidateRequest( configuration="topics", constraintsClass=TopicsConstraints::class) * Get a table of all the topics */ public function createAction(Request $request){ $categoryId = $request->request->get('category_id'); $title = $request->request->get('title'); $description = $request->request->get('description'); $messageContent = $request->request->get('message'); //... }
3. Using functions
<?php //... use RequestValidatorBundle\Custom\TopicsConstraints; //our custom constraints class use RA\RequestValidatorBundle\RequestValidator\ValidationException as RequestValidatorValidationException; class TopicController extends Controller { /** * @Get("/", name="get_all_topics") * Get a table of all the topics */ public function getAllAction(Request $request){ try { $validator = $this->get('request_validator'); $this->validator->validateQuery('topics',TopicsConstraints::class); } catch (RequestValidatorValidationException $e) { return new JsonResponse($e->getErrors(), $e->getCode()); } $categoryId = $request->get('category_id'); } /** * @Post("/", name="create_topic") * Get a table of all the topics */ public function createAction(Request $request){ try { $validator = $this->get('request_validator'); $this->validator->validateRequest('topics',TopicsConstraints::class); } catch (RequestValidatorValidationException $e) { return new JsonResponse($e->getErrors(), $e->getCode()); } $categoryId = $request->request->get('category_id'); $title = $request->request->get('title'); $description = $request->request->get('description'); $messageContent = $request->request->get('message'); }
4. Using ValidateAny
This method check both, URI and posted parameters. When using ValidateAny, the configuration defined into the constraints class can only contain a constraints collection. Here we use topics2. ValidateAny can also be used as a function in the controller body. For now, ValidateAny will merge request fields and query fields if they have the same name.
<?php //... use RequestValidatorBundle\Custom\TopicsConstraints; //our custom constraints class use RA\RequestValidatorBundle\Annotations\ValidateAny; class TopicController extends Controller { /** * @Get("/", name="get_all_topics") * @ValidateAny( configuration="topics2", constraintsClass=TopicsConstraints::class) * Get a table of all the topics */ public function getAllAction(Request $request){ $categoryId = $request->get('category_id'); //... } /** * @Post("/", name="create_topic") * @ValidateAny( configuration="topics2", constraintsClass=TopicsConstraints::class) * Get a table of all the topics */ public function createAction(Request $request){ $categoryId = $request->request->get('category_id'); $title = $request->request->get('title'); $description = $request->request->get('description'); $messageContent = $request->request->get('message'); //... }
relief_applications/symfony-request-validator-bundle 适用场景与选型建议
relief_applications/symfony-request-validator-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14.55k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 09 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「ReliefApps RA RequestValidatorBundle」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 relief_applications/symfony-request-validator-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 relief_applications/symfony-request-validator-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 14.55k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-09-23