search
HomePHP FrameworkThinkPHPHow to use AOP aspect programming in thinkphp to quickly verify our data

The following tutorial column of thinkphp will introduce to you the use of AOP aspect programming in thinkphp to quickly verify our data. I hope it will be helpful to friends in need!

How to use AOP aspect programming in thinkphp to quickly verify our data

Use AOP aspect programming in thinkphp to quickly verify our data

1) First check our directory structure

How to use AOP aspect programming in thinkphp to quickly verify our data

2) Use normal writing to construct our verification

How to use AOP aspect programming in thinkphp to quickly verify our data

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 have multiple problems. When verifying or multiple modules, there will be repetitive redundancy in writing this string of code

Question? How to compress the appeal code into one line

AOP : A technology that dynamically and uniformly adds functions to a program without modifying the source code. 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 problems by merging and unifying modules with a single function

We created

validate# under common ## directory, and create a BaseValidate file, 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;
            }
  • after optimization Code

How to use AOP aspect programming in thinkphp to quickly verify our dataI feel a lot more comfortable instantly. It saves a lot of code, because this thing can be used under many controllers. It should be used

Optimization 2

For example, in the code in baseValidate, there is a string of codes is_null, which is written to verify all the data passed up. When we need to verify When verifying all the data, you only need to write like this

How to use AOP aspect programming in thinkphp to quickly verify our data

How to use AOP aspect programming in thinkphp to quickly verify our data##The data can also be verified. But there will be a doubt. We did not get the data data. We cannot use the data data. We still need to get it again in the controller. This is not advisable, so I chose to do this

How to use AOP aspect programming in thinkphp to quickly verify our data

Related recommendations:
The latest 10 thinkphp video tutorials

The above is the detailed content of How to use AOP aspect programming in thinkphp to quickly verify our data. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.