Laravel 请求、响应、中间件与服务容器全解析
1. 中间件的使用与绑定
在 Laravel 中,中间件扮演着重要的角色,它可以对请求进行预处理,也可以对响应进行后处理。以下是一个简单的中间件示例,用于封禁特定 IP 地址:
return response('BANNED IP ADDRESS!', 403);
}
// Now we've decided to accept it. Let's pass it on to the next
// middleware in the stack. We pass it to $next(), and what is
// returned is the response after the $request has been passed
// down the stack of middleware to the application and the
// application's response has been passed back up the stack.
$response = $next($request);
// At this point, we can once again interact with the response
// just before it is returned to the user
$response->cookie('visited-our-site', true);
// Finally, we can release this response to the end user
return $response;
}
}
</
超级会员免费看
订阅专栏 解锁全文
470

被折叠的 条评论
为什么被折叠?



