Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Mini Program _ ThinkPHP_2

Shulou Source: shulou.com Published: 2022-06-03 07:59:47 11月10日 Update

Build verification layer

Download a tp5 framework file to xampp\ htdocs and rename it to zerg (server), then open a new project with PhpStorm, open a new api folder module under the application directory, then create a new controller folder, create a new v1 folder under controller, and then create a new Banner.php (Controller layer) with a new method getBanner (Action action):

Class Banner

{

Public function getBanner () {

}

}

1. Straightforward verification:

(1) TP5 transmission parameter method

Public function getBanner ($id) {

$data = [

'id' = > $id

]

$validate = new Validate (['id' = >' require | max:2' / / indicates that the id parameter must be no more than 2 characters long]); $result = $validate- > check ($data); if (! $result) {echo $validate- > getError ();} else {echo "success";}

}

The test results are as follows:

(2) the method of obtaining all parameters by Request:

The result is the same:

$validate = new Validate ([

'id' = >' require | max:2'

])

$request = Request::instance ()

$result = $validate- > check ($request- > param ())

If (! $result) {

Echo $validate- > getError ()

} else {

Echo "success"

}

two。 Object-oriented verification:

Mode 1 (prototype)

Create a new validate under api, and create a new php file IDMustBePositiveInt.php that inherits from Validate. Add the following code

Protected $rule = [

'id' = >' require | isPositiveInteger', / / add a custom verification method isPositiveInteger

]

Protected function isPositiveInteger ($value, $rule ='', $data ='', $field =') {if (is_numeric ($value) & & is_int ($value+0) & & ($value+0) > 0) {return true;} else {return $field. 'must be a positive integer';}}

The results are as follows:

Mode 2 (final version)

Create a new BaseValidate.php file (controller) under validate and inherit from Validate, and add a custom method goCheck ()

Then let isMusBePositiveInt inherit with Validate

Then all you need is one sentence in Banner.

Test result

Object-oriented programming is not only less code but also reusable, so it needs to be mastered well.

Tags: Files methods methods results validation parameters folders success code objects directories tests bluntness one sentence actions just characters controllers integers frames Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Microsoft Xiaomi NVidia Linux