寫api接口時(shí)一般會(huì)在控制器中簡(jiǎn)單驗(yàn)證參數(shù)的正確性。
使用yii只帶驗(yàn)證器(因?yàn)楸容^熟悉)實(shí)現(xiàn)有兩種方式(效果都不佳)。
針對(duì)每個(gè)請(qǐng)求單獨(dú)寫個(gè) Model , 定義驗(yàn)證規(guī)則并進(jìn)行驗(yàn)證。 缺點(diǎn):寫好多參數(shù)驗(yàn)證的 Model 類。
使用 獨(dú)立驗(yàn)證器 中提到的 $validator->validateValue()
方法直接驗(yàn)證變量值。缺點(diǎn):寫實(shí)例化很多驗(yàn)證器對(duì)象。
有么有“一勞永逸”的做法,像在 Model 中通過(guò) rules 方法定義驗(yàn)證規(guī)則并實(shí)現(xiàn)快速驗(yàn)證的呢?有!
使用方法(實(shí)現(xiàn)效果)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
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)); } //... } } |
實(shí)現(xiàn)方法
定義參數(shù)驗(yàn)證模型
定義參數(shù)驗(yàn)證模型 ParamsValidateModel
,繼承 yii\db\ActiveRecord
,重寫 attributes()
方法,主要功能:
- 驗(yàn)證規(guī)則可從對(duì)象外部進(jìn)行設(shè)置。
- 從驗(yàn)證規(guī)則中獲取可賦值的屬性。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<?php namespace common\models\app; use yii\db\ActiveRecord; class ParamsValidateModel extends ActiveRecord { /** * @var array 驗(yàn)證規(guī)則 */ private $_rules = []; private $_attributes = []; // 設(shè)置驗(yàn)證規(guī)則 public function setRules( $rules ) { $this ->_rules = $rules ; foreach ( $rules as $item ) { $this ->_attributes = array_unique ( array_merge ( $this ->_attributes, ( array ) $item [0])); } } // 重寫獲取驗(yàn)證規(guī)則 public function rules() { return $this ->_rules; } // 設(shè)置可用屬性列表 public function attributes() { return $this ->_attributes; } } |
定義參數(shù)驗(yàn)證服務(wù)類
定義參數(shù)驗(yàn)證服務(wù)類,主要功能有:
- 設(shè)置參數(shù)列表和參數(shù)規(guī)則列表。
- 使用 參數(shù)驗(yàn)證模型 進(jìn)行驗(yàn)證和存儲(chǔ)驗(yàn)證錯(cuò)誤消息。
- 使用魔術(shù)方法獲取 參數(shù)驗(yàn)證模型 中的驗(yàn)證錯(cuò)誤消息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
<?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 數(shù)據(jù)項(xiàng) * @param array $rules 驗(yàn)證規(guī)則 * @return bool */ public function validate( $data , $rules ) { // 添加驗(yàn)證規(guī)則 $this ->model->setRules( $rules ); // 設(shè)置參數(shù) $this ->model->load( $data , '' ); // 進(jìn)行驗(yàn)證 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 ); } } } |
總結(jié)
以上所述是小編給大家介紹的yii2 在控制器中驗(yàn)證請(qǐng)求參數(shù)的使用方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
原文鏈接:https://segmentfault.com/a/1190000019513532