yii2 在控制器中验证请求参数的使用方法
发布:smiling 来源: PHP粉丝网 添加日期:2021-11-27 15:03:35 浏览:3 评论:0
这篇文章主要介绍了yii2 在控制器中验证请求参数的使用方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下。
写api接口时一般会在控制器中简单验证参数的正确性。
使用yii只带验证器(因为比较熟悉)实现有两种方式(效果都不佳)。
针对每个请求单独写个 Model , 定义验证规则并进行验证,缺点:写好多参数验证的 Model 类。
使用 独立验证器 中提到的 $validator->validateValue() 方法直接验证变量值,缺点:写实例化很多验证器对象。
有么有“一劳永逸”的做法,像在 Model 中通过 rules 方法定义验证规则并实现快速验证的呢?有!
使用方法(实现效果)
- namespace frontend\controllers\api;
- use yii\web\Controller;
- use common\services\app\ParamsValidateService;
- class ArticleController extends Controller
- {
- // 文章列表
- public function actionList()
- {
- $PVS = new ParamsValidateService();
- $valid = $PVS->validate(\Yii::$app->request->get(), [
- ['category_id', 'required'],
- ['category_id', 'integer'],
- ['keyword', 'string'],
- ]);
- if (!$valid) {
- $this->apiError(1001, $PVS->getErrorSummary(true));
- }
- //...
- }
- // 新增文章
- public function actionPost()
- {
- $PVS = new ParamsValidateService();
- $valid = $PVS->validate(\Yii::$app->request->get(), [
- [['category_id', 'title', 'content'], 'required'],
- ['category_id', 'integer'],
- [['title'], 'string', 'max' => 64],
- [['content'], 'string'],
- ]);
- if (!$valid) {
- $this->apiError(1001, $PVS->getErrorSummary(true));
- }
- //...
- }
- // 文章删除
- public function actionDelete()
- {
- $PVS = new ParamsValidateService();
- $valid = $PVS->validate(\Yii::$app->request->get(), [
- ['article_id', 'required'],
- ['article_id', 'integer'],
- ]);
- if (!$valid) {
- $this->apiError(1001, $PVS->getErrorSummary(true));
- }
- //...
- }
- }
实现方法
定义参数验证模型
定义参数验证模型 ParamsValidateModel ,继承 yii\db\ActiveRecord ,重写 attributes() 方法,主要功能:
验证规则可从对象外部进行设置。
从验证规则中获取可赋值的属性。
- <?php
- namespace common\models\app;
- use yii\db\ActiveRecord;
- class ParamsValidateModel extends ActiveRecord
- {
- /**
- * @var array 验证规则
- */
- private $_rules = [];
- private $_attributes = [];
- // 设置验证规则
- public function setRules($rules)
- {
- $this->_rules = $rules;
- foreach ($rules as $item) {
- $this->_attributes = array_unique(array_merge($this->_attributes, (array)$item[0]));
- }
- }
- // 重写获取验证规则
- public function rules()
- {
- return $this->_rules;
- }
- // 设置可用属性列表
- public function attributes()
- {
- return $this->_attributes;
- }
- }
定义参数验证服务类
定义参数验证服务类,主要功能有:
设置参数列表和参数规则列表。
使用 参数验证模型 进行验证和存储验证错误消息。
使用魔术方法获取 参数验证模型 中的验证错误消息。
- <?php
- namespace common\services\app;
- use common\models\app\ParamsValidateModel;
- use yii\base\Component;
- /**
- * Class ParamsValidateService
- * @package common\services\app
- * @method array getErrors(\string $attribute)
- * @method array getFirstErrors()
- * @method array getFirstError(\string $attribute)
- * @method array getErrorSummary(\boolean $showAllErrors)
- */
- class ParamsValidateService extends Component
- {
- /**
- * @var ParamsValidateModel 模型
- */
- private $model = null;
- public function init()
- {
- parent::init();
- $this->model = new ParamsValidateModel();
- }
- /**
- * @param array $data 数据项
- * @param array $rules 验证规则
- * @return bool
- */
- public function validate($data, $rules)
- {
- // 添加验证规则
- $this->model->setRules($rules);
- // 设置参数
- $this->model->load($data, '');
- // 进行验证
- return $this->model->validate();
- }
- public function __call($name, $params)
- {
- if ($this->model->hasMethod($name)) {
- return call_user_func_array([$this->model, $name], $params);
- } else {
- return parent::__call($name, $params);
- }
- }
- }
Tags: yii2验证请求参数

推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)