Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mini Program _ ThinkPHP_2

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report