How to use Thinkphp5.1 middleware
This article introduces the relevant knowledge of "how to use Thinkphp5.1 middleware". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. the role of middleware
Middleware is mainly used to intercept or filter HTTP requests of applications and carry out necessary business processing. For example, middleware can be used to check whether the user's request information contains a sentence Trojan horse.
The difference between behavior hooks and middleware:
Middleware: it processes the project request. When the user visits our project, the middleware can judge whether the user has permission or whether there is illegal access to the request.
Behavior hook: a method that is triggered at the beginning or end of an action, such as logging the success of user registration
Middleware is the processing of user requests, while hooks are the processing of user actions, middleware is equivalent to filters, hooks are equivalent to events, all adopt the idea of AOP.
Second, define middleware
First, you can quickly generate the middleware through the cmd command line instruction, and execute the following cmd command in the root directory of the project:
Php think make:middleware Check
This command generates a Check middleware under the application/http/middleware directory with the following code: