Home > Article > Backend Development > php what is middleware
Middleware, as the name suggests, intercepts and processes request data, verifies data, and determines whether to allow entry to the next middleware after logical processing between requests and responses.
Middleware is divided into prefix middleware and post-middleware. Can be used for permission authentication, logging, etc.
Middleware provides a convenient mechanism for filtering HTTP requests entering the application. (Recommended learning: PHP programming from entry to proficiency)
For example, Laravel has a built-in middleware to verify user authentication. If the user is not authenticated, the middleware will redirect the user to the login interface. However, if the user is authenticated, the middleware will allow the request further into the application.
Of course, in addition to identity authentication, you can also write additional middleware to perform various tasks.
For example: CORS middleware can be responsible for adding appropriate header information to all responses leaving the application; logging middleware can record all requests coming into the application.
Example
<?php // 框架核心应用层 $application = function($name) { echo "this is a {$name} application\n"; }; // 前置校验中间件 $auth = function($handler) { return function($name) use ($handler) { echo "{$name} need a auth middleware\n"; return $handler($name); }; }; // 前置过滤中间件 $filter = function($handler) { return function($name) use ($handler) { echo "{$name} need a filter middleware\n"; return $handler($name); }; }; // 后置日志中间件 $log = function($handler) { return function($name) use ($handler) { $return = $handler($name); echo "{$name} need a log middleware\n"; return $return; }; }; // 中间件栈 $stack = []; // 打包 function pack_middleware($handler, $stack) { foreach (array_reverse($stack) as $key => $middleware) { $handler = $middleware($handler); } return $handler; } // 注册中间件 // 这里用的都是全局中间件,实际应用时还可以为指定路由注册局部中间件 $stack['log'] = $log; $stack['filter'] = $filter; $stack['auth'] = $auth; $run = pack_middleware($application, $stack); $run('Laravle');
Output:
Laravle need a filter middleware Laravle need a auth middleware this is a Laravle application Laravle need a log middleware
Packaging program
The execution sequence of middleware is given by The packaging function (pack_middleware) determines that the closure returned here is actually equivalent to:
$run = $log($filter($auth($application))); $run('Laravle');
Writing specifications
Middleware must meet certain specifications: always return A closure, the same parameters are always passed in the closure (determined by the main logic), and the closure always returns the execution result of the handler;
If the logic of the middleware returns the handle return $ If it is completed before handler($name), it is pre-middleware, otherwise it is post-middleware.
The above is the detailed content of php what is middleware. For more information, please follow other related articles on the PHP Chinese website!