ThinkPHP implementation method of parameter verification similar to AOP idea

Time:2021-11-22

Explanation of ideas: no matter when developing APIs or doing background projects, the back end should never trust the parameters transmitted by the front end. What it usually needs to do is to verify the legitimacy and security of the parameters. So how to easily verify the parameters in the actual project development. TP provides several methods of parameter verification, such as verifier, independent verification, or using the validate method when inheriting the controller base class. In contrast, the verifier is the best choice. A controller has multiple methods, which means that there are multiple requests, which means that there are multiple scenarios. If a project has more than one controller, it means that more than one verifier needs to be established. For the object-oriented idea, we need to establish a base class validator, and then let the subclass inherit. So how to implement parameter verification? Let me introduce the implementation of parameter verification similar to AOP.

Define verifier base class

Define the base class app \ common \ validator \ basevalidator.php

<?php

namespace app\common\validator;

use app\common\exception\ParamException;
use think\Validate;

class BaseValidator extends Validate
{
  /**
   * @param string $scene
   * @return bool
   * @throws ParamException
   */
  public function checkParams($scene='')
  {
    $params = input('param.');
    $res = $this->scene($scene)->check($params);

    if( ! $res ){
      $error = $this->error;
      if(is_array($error)){
        $error = implode(',',$error);
      }
      throw new ParamException(['errMsg'=>$error,]);
    }
    return $res;
  }

  //Custom validation rules

}

Define validator

<?php

namespace app\common\validator\user;

use app\common\validator\BaseValidator;

class UserValidator extends BaseValidator
{
  protected $rule =  [
    'name' => 'require|max:25',
    'age'  => 'number|between:1,120',
    'email' => 'email',
  ];

  protected $message =  [
    'name. Require' = > 'name must',
    'name. Max' = > 'the name cannot exceed 25 characters at most',
    'age. Number' = > 'age must be a number',
    'age. Between' = > 'age can only be between 1-120',
    'email' = > 'email format error',
  ];

  protected $scene = [
    'register' => ['name','email'],
  ];
}

Validation parameters

The user.php controller uses the register method to instantiate the verifier and perform scenario verification.


  public function register(Request $request){
    $validator = new UserValidator();
    $validator->checkParams('register');
    .
    .
    .
  }  

So far, the parameter verification similar to the AOP idea is completed.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

Apache sqoop

Source: dark horse big data 1.png From the standpoint of Apache, data flow can be divided into data import and export: Import: data import. RDBMS—–>Hadoop Export: data export. Hadoop—->RDBMS 1.2 sqoop installation The prerequisite for installing sqoop is that you already have a Java and Hadoop environment. Latest stable version: 1.4.6 Download the sqoop installation […]