Home >PHP Framework >ThinkPHP >What are the differences between the front-end middleware and the back-end middleware of ThinkPHP6?
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:
<?php namespace app\middleware; class Before { public function handle($request, \Closure $next) { // 添加中间件执行代码 return $next($request); } }
Post-middleware:
<?php namespace app\middleware; class After { public function handle($request, \Closure $next) { $response = $next($request); // 添加中间件执行代码 return $response; } }
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.
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?
<?php namespace app\middleware; class After { public function handle($request, \Closure $next) { $response = $next($request); // 添加中间件执行代码 if(empty('session')){ echo '登录不合法'; //跳转到登录页面 } return $response; } }
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!