设为首页 加入收藏

TOP

laravel中间件的实现原理
2019-09-17 15:04:44 】 浏览:59
Tags:laravel 中间件 实现 原理
中间件的实现原理

运用 array_reduce 以及 call_user_func 实现

interface Middleware
{
    public static function handle(Closure $next);
}

class VerfiyCsrfToekn implements Milldeware{    
    public static function handle(Closure $next)    {        
        echo '验证csrf Token <br>';        
        $next();    
    }
}

class VerfiyAuth implements Milldeware{
    public static function handle(Closure $next)    {        
        echo '验证是否登录 <br>';        
        $next();    
    }
}

class SetCookie implements Milldeware{    
    public static function handle(Closure $next)    {        
        $next();        
        echo '设置cookie信息!';    
    }
}

$handle = function () {    
    echo '当前要执行的程序!';
};

$pipe_arr = [ 'VerfiyCsrfToekn', 'VerfiyAuth', 'SetCookie', ];

$callback = array_reduce($pipe_arr, function ($stack, $pipe) {
    return function () use ($stack, $pipe) {
        return $pipe::handle($stack);
    };
}, $handle);

call_user_func($callback);
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇文件上传报错:Unknown: file cre.. 下一篇身份证校验-----15位,18位后端

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目