This article brings you an introduction to the method of quickly verifying data using AOP aspect programming in thinkphp. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1) First check our directory structure
#2) Use normal writing to construct our verification
This is mainly divided into four steps. Compared with using independent validators or data verification one by one, most of the code has been optimized. However, when using it, we will find that we are in multiple verifications or multiple verifications. When writing a module, there will be repetitive redundancy in writing this string of code
Question? How to compress the appeal code into one line
AOP: Without modifying the source code A technology that dynamically and uniformly adds functionality to a program. AOP is actually a continuation of the GoF design pattern. The design pattern tirelessly pursues the decoupling between the caller and the callee, improving the flexibility and scalability of the code. AOP can be said to be a realization of this goal## Although #AOP and OOP are very similar literally, they are two design ideas for different fields. OOP (Object-Oriented Programming) abstractly encapsulates the entities of the business processing process and their attributes and behaviors to obtain a clearer and more efficient division of logical units.
BaseValidate.php
<?php namespace app\common\validate; use app\common\controller\Base; use think\Request; use think\Validate; class BaseValidate extends Validate { /** * 基础类控制器 * @param null|array $data * @return bool */ public function goCheck($data = null) { # 当 data 不存在的时候去自动校验获取到的参数 if( is_null($data) ) { # 获取待验证的参数 $data = Request::instance()->param(); } # 进行验证 if( !$this->check($data) ) { (new Base())->ajaxjson(Base::error, $this->getError()); # 抛出的自定义异常 } return true; }
PHP Video Tutorial column on the PHP Chinese website!
The above is the detailed content of Introduction to the method of using AOP aspect programming to quickly verify data in thinkphp. For more information, please follow other related articles on the PHP Chinese website!