May 2019

When writing API with ThinkPaPHP, under the same module, some methods (functions) need to verify login, and some methods (functions) do not need to verify login. Then I used "stupid methods", and the methods (functions) that need to verify login have to write a few lines to determine whether to log in, resulting in code redundancy and low code quality. I read carefully today ThinkPHP 5.1 Develop documents and learn a new skill middleware( https://www.kancloud.cn/manual/thinkphp5_1/564279

In this record, because of the lack of in-depth understanding of the ThinkPaP framework, it is inevitable that there are errors. Please Dalao point out how to correct them????


Create middleware

Think PHP 5.1 For detailed usage of middleware, please see the official document: https://www.kancloud.cn/manual/thinkphp5_1/564279

Method 1:

Use the command to create a quick creation (you need to set PHP variables, you can't understand this, please see the next method):

 php think make:middleware Auth

If no accident occurs, the following prompt will be returned:

E:PhpStorm_Projectx.nowtime.cc>php think make:middleware Auth
Middleware created successfully.

Method 2:

Create Manually application/http/middleware/Auth.php file

Write code

edit application/http/middleware/Auth.php File, write business logic:


-Read the rest-