Home  >  Article  >  PHP Framework  >  Let’s talk about the various middleware in ThinkPHP6

Let’s talk about the various middleware in ThinkPHP6

王雪芹
王雪芹Original
2020-05-06 17:18:082150browse

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:
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