search
HomePHP FrameworkThinkPHPWhat is the usage of thinkphp middleware?

In thinkphp, the class files created in the "APP\middleware" directory are middleware, which are mainly used to intercept or filter HTTP requests of applications and perform necessary business processing. They can be divided into global middleware , routing middleware and controller middleware.

What is the usage of thinkphp middleware?

The operating environment of this article: Windows 10 system, ThinkPHP version 5, Dell G3 computer.

What is the usage of thinkphp middleware

1. Middleware file definition

Create in the APP\middleware directory The class files are all middleware. Global middleware, routing middleware, and controller middleware can all be referenced through directory paths. Multiple applications should create a new middleware directory in their own application directory, and then create class files below.

Middleware is mainly used to intercept or filter application HTTP requests and perform necessary business processing.

Define middleware: You can quickly generate middleware through command line instructions

php think make:middleware Check

This instruction will generate a Check middleware under the application/http/middleware directory.

2. Middleware file reference or use

After creating the middleware file in one step

(1) Use

as a global middleware. In the middleware.php file in the APP directory, add the path and file in 1, such as \app\middleware\chushimima::class, this is as Use global middleware. It works globally.

Middleware that is not registered in middleware.php is not global middleware. It is divided into routing middleware and controller middleware according to different definition methods.

(2) Use as routing middleware

Still the same middleware class file \app\middleware\chushimima, the route in the route directory under the APP directory In the definition file, 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) Use as controller middleware

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

class Index extends baseController
{ protected $middleware='\app\middleware\chushimima';

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the usage of thinkphp middleware?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft