Home PHP Framework ThinkPHP Let's talk about the various middleware in ThinkPHP6

Let's talk about the various middleware in ThinkPHP6

May 06, 2020 pm 05:18 PM
thinkphp6

There is a new middleware function in ThinkPHP6, and middleware is divided into many types of middleware. Many novices have difficulty with ThinkPHP6 middleware. I will introduce them to you below.

Global middleware:

After we download the ThinkPHP6 framework, there will be a middleware.php in the app directory. We will The middleware defined in the directory is called global middleware. Global middleware is middleware that is effective for all applications.

Application middleware:

In other words, if we copy another copy of middleware.php and place it under an application , such as index application, is it called application middleware? The answer is correct.

Controller middleware:

If middleware is operated in the controller, is it called controller middleware?

Routing middleware:

Can I also define middleware in routing? For example, I only want a certain request If you use certain middleware and others don't want to use middleware, then there is routing middleware.

If all types of middleware are defined for the same request, what is the execution order?

Global middleware->Application middleware->Routing middleware->Controller middleware

Next we look at an example.

Global middleware:

 public function handle($request, \Closure $next)
    {
        echo 'app全局中间件';
        return $next($request);
    }

Application middleware:

public function handle($request, \Closure $next)
    {
        echo '应用中间件';
        return $next($request);
    }

Routing middleware:

Route::rule('hello','index/hello')
->middleware(\app\middleware\Auth::class);

Controller Middleware:

Before using the controller middleware, we first define the alias and define the alias in config\middleware.php in the root directory. This alias is actually to add an alias identifier. After the alias is defined, it can be used not only in controller middleware, but also in routing middleware.

return [
    'alias' => [
        'auth1' => app\middleware\Auth1::class,
    ],
];

When we define the alias middleware here, we don’t need to declare it in middleware.php. Instead, we use the alias definition in the control:

class Index
{
    protected $middleware = ['auth1'];
    public function index()
    {
       return 'index';
    }
    public function hello()
    {
       echo 'hello方法';
    }
}

At this time we access the hello method, The output result is:

app global middleware

application middleware

routing middleware

controller middleware

hello method

The above is an introduction to the various middlewares of ThinkPHP6. In fact, the various middlewares are not difficult to understand. The main thing is that everyone needs to be clear about the execution order between them.

The above is the detailed content of Let's talk about the various middleware in ThinkPHP6. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)