Home>Article>PHP Framework> What are the differences between the front-end middleware and the back-end middleware of ThinkPHP6?

What are the differences between the front-end middleware and the back-end middleware of ThinkPHP6?

王雪芹
王雪芹 Original
2020-05-06 16:12:58 3365browse

In the last example, we learned to define middleware and understood the meaning of middleware. Then we flipped through the manual and found another one called "pre-middleware" and "post-middleware". This is what's the situation? What is the difference between the two?

1. The difference in definitions.

It is not difficult to find from the official website manual that the definitions of pre-middleware and post-middleware are different. Let’s take a look below.

Pre-middleware definition:


      

Post-middleware:


      

After a closer look, I found that in the final analysis, the two are execution code and $next when they are defined. ($request) order problem, it is this order that is the key to pre-middleware and post-middleware.

2. $request is different.

We can print the following codes in the pre-middleware and post-middleware respectively:

halt($request);

We carefully compared and found that the controllers and methods in the pre-middleware are Empty, the post-middleware can get the controller name and method. So if we want to get the currently accessed controller and method, we need to use post-middleware to solve it.

What are the differences between the front-end middleware and the back-end middleware of ThinkPHP6?What are the differences between the front-end middleware and the back-end middleware of ThinkPHP6?

3. Middleware intercepts login scenario.

Let’s first take a look at the definition of middleware on the official website:

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

Seeing the definition of the official website, friends may think that it is really best to use middleware for login interception. But should we use pre- or post-middleware?


      

Backend home page:

public function index(){ echo '后台首页'; }

The execution results will find that we can still execute the index method before the login jump, and can output the content.

Therefore, post-middleware cannot be used in login scenarios. Pre-middleware is the best solution for intercepting logins. At this time, it is also necessary to determine whether the current access is login. If it is login, there will be many redirect, so friends must handle this well.

The above is the relevant introduction to pre- and post-middleware. In actual projects, we determine whether to use pre- or post-middleware based on our actual business logic.

The above is the detailed content of What are the differences between the front-end middleware and the back-end middleware of 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