Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the middleware of thinkphp

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you the relevant knowledge of how to use thinkphp middleware. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

In thinkphp, the class files established in the "APP\ middleware" directory are middleware, which are mainly used to intercept or filter application HTTP requests and carry out necessary business processing, which can be divided into global middleware, routing middleware and controller middleware.

This article operating environment: Windows10 system, ThinkPHP5 version, Dell G3 computer.

What is the use of thinkphp middleware

I. definition of middleware files

The class files created in the APP\ middleware directory are all middleware. Global middleware, routing middleware, and controller middleware can all reference these middleware through the directory path. Multiple applications should create a new middleware directory under their own application directory, and then create class files below.

Middleware is mainly used to intercept or filter HTTP requests of applications and carry out necessary business processing.

Define middleware: middleware can be generated quickly through command line instructions

Php think make:middleware Check

This command generates a Check middleware under the application/http/middleware directory.

II. Reference or use of middleware files

After setting up the middleware file in one step

(1) to be used as a global component

In the middleware.php file in the APP directory, add the path and file in one, such as\ app\ middleware\ chushimima::class, which is used as global middleware. It works in the overall situation.

The middleware that is not registered in middleware.php is not global middleware, but can be divided into routing middleware and controller middleware according to different definition methods.

(2) used as routing middleware

It is also the same middleware class file\ app\ middleware\ chushimima. In the routing definition file under the route directory under the APP directory, add routing rules, such as

Route::rule ('index/denglu','index/denglu');-> middleware ('\ app\ middleware\ chushimima')

This is the routing middleware, which only works when accessing this route.

(3) used as controller middleware

Again, the same middleware class file\ app\ middleware\ chushimima, add the following code to your controller file, such as

Class Index extends baseController {protected $middleware='\ app\ middleware\ chushimima'; above is all the content of this article "how to use thinkphp middleware". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report