Skip to content

Instantly share code, notes, and snippets.

@storyflow
Last active August 16, 2018 09:17
Show Gist options
  • Save storyflow/a7290e03aeb3a1e5c578fa6ccd809758 to your computer and use it in GitHub Desktop.
Save storyflow/a7290e03aeb3a1e5c578fa6ccd809758 to your computer and use it in GitHub Desktop.
$db = function ($request, Closure $next) {
echo '成功建立数据库连接' . PHP_EOL;
$response = $next($request);
echo '成功关闭数据库连接' . PHP_EOL;
return $response;
};
$like = function ($request, Closure $next) {
echo '点赞+1' . PHP_EOL;
$response = $next($request);
echo '点赞+2' . PHP_EOL;
return $response;
};
$app = function ($request) {
echo $request . PHP_EOL;
return '一个无聊的返回值';
};
$allMiddleware = [$like, $db];
$next = $app;
foreach ($allMiddleware as $middleware) {
$next = function ($request) use ($middleware, $next) {
return $middleware($request, $next);
};
}
$response = $next('O(∩_∩)O');
echo $response;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment