search
HomePHP FrameworkThinkPHPGet started with ThinkPHP6 middleware with a simple example

Starting from ThinkPHP6, a term called "middleware" appears in the official manual. So what is middleware used for? How should it be used? Let’s analyze it slowly below.

First look at the definition of the middleware official website:

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

It is not difficult to understand from the literal meaning. Middleware can intercept or do other things before we request the controller's method.

We write a line of code in the index method in the index control in www.blog.com:

echo "index方法";

Let’s test what will happen if the middleware is defined?

1. Definition of middleware

After we download the ThinkPHP6 framework, there is middleware.php under the app. This file is the middleware Definition file, we pay attention to its location. It is not placed under any application, but in the same directory as BaseController.php under app, so it is a global middleware.

Defining middleware is very simple. We have two methods:

1. Use the command line. php think make:middleware Check. My feeling after using this is just one word: fast! so fast! Middleware definition is completed instantly.

2. Copy and paste, we create the middleware directory under the app, and create a Check.php middleware:

<?php
namespace app\middleware;
class Check
{
    public function handle($request, \Closure $next)
    {
        echo "app中间件";
        return $next($request);
    }
}

The middleware is defined, why does it not take effect?

Follow the above middleware definition steps, the middleware definition is completed, but it does not take effect. What is the reason?

The reason is that it is not started in the configuration file. How to do it?

Open app\middleware.php and add the following code to the minimum page:

app\middleware\Check::class

When we visit www.blog.com again, we will find that the content of the middleware is output first, and then Enter the contents of the controller.

Get started with ThinkPHP6 middleware with a simple example

This also conforms to the official definition of middleware, which is to execute the corresponding middleware before executing the method. The above is a small example for getting started with middleware. After understanding this small example, it will open the door to other middleware contents.

The above is the detailed content of Get started with ThinkPHP6 middleware with a simple example. 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.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

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.

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),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment