php简单的中间件实现
// 框架核心应用层
application = function(name) {
    echo "this is a {name} application\n" . PHP_EOL;
};

// 前置校验中间件auth = function(handler) {
    return function(name) use (handler) {
        echo "{name} need a auth middleware\n" . PHP_EOL;
        return handler(name);
    };
};

// 前置过滤中间件
filter = function(handler) {
    return function(name) use (handler) {
        echo "{name} need a filter middleware\n" . PHP_EOL;
        returnhandler(name);
    };
};

// 后置日志中间件log = function(handler) {
    return function(name) use (handler) {return = handler(name);
        echo "{name} need a log middleware\n" . PHP_EOL;
        returnreturn;
    };
};

// 中间件栈
stack = [];

// 打包
function pack_middleware(handler, stack)
{
    foreach (array_reverse(stack) as key =>middleware) 
    {
        handler =middleware(handler);
    }
    returnhandler;
}

// 注册中间件
// 这里用的都是全局中间件,实际应用时还可以为指定路由注册局部中间件
stack['log'] =log;
stack['filter'] =filter;
stack['auth'] =auth;

run = pack_middleware(application, stack);run('Laravle');
暂无评论

发送评论 编辑评论


|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇