relief_applications / symfony-request-validator-bundle
dev-master
2018-07-03 07:24 UTC
Requires
- php: >=7.0
- symfony/framework-bundle: >=2.5.6
This package is auto-updated.
Last update: 2024-09-11 00:20:35 UTC
README
步骤 1: 下载 Bundle
打开命令行控制台,进入您的项目目录并执行以下命令以下载此 Bundle 的最新稳定版本
$ composer require relief_applications/symfony-request-validator-bundle @dev
此命令要求您已全局安装 Composer,如 Composer 文档中的安装章节中所述。
步骤 2: 启用 Bundle
然后,通过将其添加到项目 app/AppKernel.php
文件中注册的 Bundle 列表中来启用该 Bundle
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new RA\RequestValidatorBundle\RARequestValidatorBundle(), ); } }
步骤 3: 配置
# app/config/services.yml according to your project path imports: - { resource: "@RARequestValidatorBundle/Resources/config/services.yml" }
步骤 4: 默认路由
要测试我们的默认控制器
# app/config/routing.yml according to your project path RequestValidatorBundle: resource: "@RARequestValidatorBundle/Resources/config/routing.yml"
步骤 5: 使用方法
1. 定义自定义约束类
此类应扩展 RA\RequestValidatorBundle\RequestValidator\Constraints。约束列表可在以下位置找到 https://symfony.com.cn/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. 使用注解
<?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. 使用函数
<?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. 使用 ValidateAny
此方法检查 URI 和提交的参数。当使用 ValidateAny 时,约束类中定义的配置只能包含约束集合。这里我们使用 topics2。ValidateAny 还可以用于控制器体中的函数。 目前,ValidateAny 将合并具有相同名称的请求字段和查询字段。
<?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'); //... }