当前位置:首页 > CMS教程 > 其它CMS > 列表

yii2 在控制器中验证请求参数的使用方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-11-27 15:03:35 浏览: 评论:0 

这篇文章主要介绍了yii2 在控制器中验证请求参数的使用方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下。

写api接口时一般会在控制器中简单验证参数的正确性。

使用yii只带验证器(因为比较熟悉)实现有两种方式(效果都不佳)。

针对每个请求单独写个 Model , 定义验证规则并进行验证,缺点:写好多参数验证的 Model 类。

使用 独立验证器 中提到的 $validator->validateValue() 方法直接验证变量值,缺点:写实例化很多验证器对象。

有么有“一劳永逸”的做法,像在 Model 中通过 rules 方法定义验证规则并实现快速验证的呢?有!

使用方法(实现效果)

  1. namespace frontend\controllers\api; 
  2. use yii\web\Controller; 
  3. use common\services\app\ParamsValidateService; 
  4. class ArticleController extends Controller 
  5. { 
  6.   // 文章列表 
  7.   public function actionList() 
  8.   { 
  9.     $PVS = new ParamsValidateService(); 
  10.     $valid = $PVS->validate(\Yii::$app->request->get(), [ 
  11.       ['category_id', 'required'], 
  12.       ['category_id', 'integer'], 
  13.       ['keyword', 'string'], 
  14.     ]); 
  15.     if (!$valid) { 
  16.       $this->apiError(1001, $PVS->getErrorSummary(true)); 
  17.     } 
  18.     //... 
  19.   } 
  20.   // 新增文章 
  21.   public function actionPost() 
  22.   { 
  23.     $PVS = new ParamsValidateService(); 
  24.     $valid = $PVS->validate(\Yii::$app->request->get(), [ 
  25.       [['category_id', 'title', 'content'], 'required'], 
  26.       ['category_id', 'integer'], 
  27.       [['title'], 'string', 'max' => 64], 
  28.       [['content'], 'string'], 
  29.     ]); 
  30.     if (!$valid) { 
  31.       $this->apiError(1001, $PVS->getErrorSummary(true)); 
  32.     } 
  33.     //... 
  34.   } 
  35.   // 文章删除 
  36.   public function actionDelete() 
  37.   { 
  38.     $PVS = new ParamsValidateService(); 
  39.     $valid = $PVS->validate(\Yii::$app->request->get(), [ 
  40.       ['article_id', 'required'], 
  41.       ['article_id', 'integer'], 
  42.     ]); 
  43.     if (!$valid) { 
  44.       $this->apiError(1001, $PVS->getErrorSummary(true)); 
  45.     } 
  46.     //... 
  47.   } 
  48. } 

实现方法

定义参数验证模型

定义参数验证模型 ParamsValidateModel ,继承 yii\db\ActiveRecord ,重写 attributes() 方法,主要功能:

验证规则可从对象外部进行设置。

从验证规则中获取可赋值的属性。

  1. <?php 
  2. namespace common\models\app; 
  3. use yii\db\ActiveRecord; 
  4. class ParamsValidateModel extends ActiveRecord 
  5. { 
  6.   /** 
  7.    * @var array 验证规则 
  8.    */ 
  9.   private $_rules = []; 
  10.   private $_attributes = []; 
  11.   // 设置验证规则 
  12.   public function setRules($rules) 
  13.   { 
  14.     $this->_rules = $rules; 
  15.     foreach ($rules as $item) { 
  16.       $this->_attributes = array_unique(array_merge($this->_attributes, (array)$item[0])); 
  17.     } 
  18.   } 
  19.   // 重写获取验证规则 
  20.   public function rules() 
  21.   { 
  22.     return $this->_rules; 
  23.   } 
  24.   // 设置可用属性列表 
  25.   public function attributes() 
  26.   { 
  27.     return $this->_attributes; 
  28.   } 
  29. } 

定义参数验证服务类

定义参数验证服务类,主要功能有:

设置参数列表和参数规则列表。

使用 参数验证模型 进行验证和存储验证错误消息。

使用魔术方法获取 参数验证模型 中的验证错误消息。

  1. <?php 
  2. namespace common\services\app; 
  3. use common\models\app\ParamsValidateModel; 
  4. use yii\base\Component; 
  5. /** 
  6.  * Class ParamsValidateService 
  7.  * @package common\services\app 
  8.  * @method array getErrors(\string $attribute) 
  9.  * @method array getFirstErrors() 
  10.  * @method array getFirstError(\string $attribute) 
  11.  * @method array getErrorSummary(\boolean $showAllErrors) 
  12.  */ 
  13. class ParamsValidateService extends Component 
  14. { 
  15.   /** 
  16.    * @var ParamsValidateModel 模型 
  17.    */ 
  18.   private $model = null; 
  19.   public function init() 
  20.   { 
  21.     parent::init(); 
  22.     $this->model = new ParamsValidateModel(); 
  23.   } 
  24.   /** 
  25.    * @param array $data 数据项 
  26.    * @param array $rules 验证规则 
  27.    * @return bool 
  28.    */ 
  29.   public function validate($data, $rules) 
  30.   { 
  31.     // 添加验证规则 
  32.     $this->model->setRules($rules); 
  33.     // 设置参数 
  34.     $this->model->load($data, ''); 
  35.     // 进行验证 
  36.     return $this->model->validate(); 
  37.   } 
  38.   public function __call($name, $params) 
  39.   { 
  40.     if ($this->model->hasMethod($name)) { 
  41.       return call_user_func_array([$this->model, $name], $params); 
  42.     } else { 
  43.       return parent::__call($name, $params); 
  44.     } 
  45.   } 
  46. }

Tags: yii2验证请求参数

分享到: