Home >Backend Development >PHP Tutorial >Introduction to the method of using AOP aspect programming to quickly verify data in thinkphp

Introduction to the method of using AOP aspect programming to quickly verify data in thinkphp

不言
不言forward
2019-03-26 11:03:132349browse

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

Introduction to the method of using AOP aspect programming to quickly verify data in thinkphp

#2) Use normal writing to construct our verification

Introduction to the method of using AOP aspect programming to quickly verify data in thinkphp

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.

We introduced the idea of ​​​​aop programming to solve our problem by merging and unifying modules with single functions.

We created the validate directory under common and created a BaseValidate file to inherit think \validate

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;
        }
Optimized code

Introduction to the method of using AOP aspect programming to quickly verify data in thinkphp

I feel a lot more comfortable instantly, saving a lot of code, because this thing is used in many There are all things that should be used under the controller.

Optimization 2

For example, the code in baseValidate, one of the codes is is_null, which is written to verify all the data passed up. When we need to verify all the data, we only need to write

Introduction to the method of using AOP aspect programming to quickly verify data in thinkphp

. The data can also be verified, but there will be a doubt. We have not obtained the data data and cannot To use data data, you still need to re-obtain it in the controller, which is not advisable, so I chose to do this

Introduction to the method of using AOP aspect programming to quickly verify data in thinkphp

Introduction to the method of using AOP aspect programming to quickly verify data in thinkphp

This article has ended here. For more other exciting content, you can pay attention to the

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!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete