php what is middleware

(*-*)浩
Release: 2023-02-24 11:38:01
Original
4556 people have browsed it

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.

php what is middleware

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[&#39;log&#39;] = $log;
$stack[&#39;filter&#39;] = $filter;
$stack[&#39;auth&#39;] = $auth;
 
$run = pack_middleware($application, $stack);
$run(&#39;Laravle&#39;);
Copy after login

Output:

Laravle need a filter middleware
Laravle need a auth middleware
this is a Laravle application
Laravle need a log middleware
Copy after login

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(&#39;Laravle&#39;);
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template