Home  >  Article  >  PHP Framework  >  Get started with ThinkPHP6 middleware with a simple example

Get started with ThinkPHP6 middleware with a simple example

王雪芹
王雪芹Original
2020-05-06 15:47:452217browse

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
Previous article:tp file uploadNext article:tp file upload